スキップしてメイン コンテンツに移動

投稿

C#: Download All Image Links from Html Page

I know this is not perfect way. But I would like to give hints to anyone who would like to download all links in html page. Example usage is something like this. // only download "jpg" files HttpUtils.SaveFirstLevelLinksToFile("", Encoding.UTF8, "c:/temp/", link => link => ".jpg".Equals(Path.GetExtension(link.Link), StringComparison.OrdinalIgnoreCase) ); Main code is below. // main entry point method public static void SaveFirstLevelLinksToFile(string baseUri, Encoding enc, string dir, Func<LinkAttr, bool> filter) { ProcessAllExtractedLinksInHtmlText(GetPage(baseUri, enc), link => { try { if (!filter(link)) return; Uri uri = ConvertToAbsoluteURL(baseUri, link.Link); var filePath = dir + uri.AbsoluteUri.GetFileName().Replace("?", ""); uri.AbsoluteUri.GetAndSaveToFile(filePath);

ActionScript 3.0: Loading Bytes from URL

I wrote a utility class for loading bytes from URL because the official method needs a lot of preparation. Here is the usage. var loader:ByteLoadHelper = new ByteLoadHelper(); loader.addEventListener(ByteLoadEvent.COMPLETE, completeHandler); loader.load(url); function completeHandler(evt:ByteLoadEvent):void { loader.removeEventListener(ByteLoadEvent.COMPLETE, completeHandler); var bytes:ByteArray = evt.data; // do something nice :D } Here is the core code. package utils.tool { import flash.events.Event; import flash.events.EventDispatcher; import flash.net.URLLoader; import flash.net.URLLoaderDataFormat; import flash.net.URLRequest; import flash.utils.ByteArray; public class ByteLoadHelper extends EventDispatcher { public function ByteLoadHelper() { } public function load(url:String):void { var urlLoader:URLLoader = new URLLoader(); urlLoader.dataFormat = URLLoa

ActionScript 3.0: Dictionary Utility Methods (Delete All Entries, Count All Entries, etc.))

public class DictionaryUtils { public function DictionaryUtils() { } public static function forEachBySortedValue(dict:Dictionary, compare:Function, callback:Function):void { var v:Vector.<Object> = extractValues(dict); v.sort(compare); for each(var value:Object in v) { callback(value); } } public static function forEachBySortedKey(dict:Dictionary, compare:Function, callback:Function):void { var v:Vector.<Object> = extractKeys(dict); v.sort(compare); for each(var key:Object in v) { callback(key, dict[key]); } } public static function extractValues(dict:Dictionary):Vector.<Object> { var v:Vector.<Object> = new Vector.<Object>(); for each(var value:Object in dict) { v.push(value); } return v; } public static function extractKeys(dict:Dictionary):Vector.<Object> { var v:Vector.<Object> = new Vector.<Object>(); for (var key:Object in dict) { v.push(key); } return v; } pu

ActionScript 3.0: Digging Directory Recursively And Process Each File

This class aims to parse directory and do something for each file. You should just override proper methods, then call process method like this. new DirectoryDigger().process("c:/temp"); package utils.file { import flash.filesystem.File; public class DirectoryDigger { public function DirectoryDigger() { } public function process(path:String):Object { var file:File = new File(path); preProcess(file); processBody(file); return postProcess(file); } protected function processBody(file:File):void { if (file.isDirectory) { preProcessDir(file); var files : Array = file.getDirectoryListing(); for (var i : int = 0; i < files.length; i++) { processBody(files[i] as File); } postProcessDir(file);

C#: Capture Screen

Here is the code for capturing current screen in given Rectangle region. public static Bitmap CaptureFromScreen(Rectangle rc) { var bmp = new Bitmap(rc.Width, rc.Height, PixelFormat.Format32bppArgb); using (var g = Graphics.FromImage(bmp)) { g.CopyFromScreen(rc.X, rc.Y, 0, 0, rc.Size, CopyPixelOperation.SourceCopy); } return bmp; } You can give something like below as a Rectangle argument // full screen area Screen.PrimaryScreen.Bounds; // 640 x 480 rectamgle from left side corner new Rectangle(0, 0, 640, 480); // working area Screen.PrimaryScreen.WorkingArea; The following links are helpful. http://homepage1.nifty.com/yasunari/VB/VB2005/DrawToScreen.htm : Sorry this page is written in Japanese... http://www.bobpowell.net/capture.htm http://msdn.microsoft.com/en-us/library/dd144871(VS.85).aspx http://msdn.microsoft.com/en-us/library/dd162920(VS.85).aspx

Solving Unimodal Function by Brent's Method

Introduction I faced a mathematical problem for solving a root from a unimodal function like quadratic function. Of course we can solve the root if the function is mono decreasing or mono increasing in the target range. However if the function is in potentially there are 2 solutions or no solutions in the target range. I know this article is not perfect yet. I will keep improving this article :) Main Requirements There are mainly 2 requirements when solving the root. Minimize the function call as much as possible: because the function needs heavy calculation Robustness against oscillating: Maybe the below figure helps your understanding. If we solve the root strictly, there can be a lot of solutions in the range because of the small oscillating. However we should choose reasonable accrual solution from candidate points. Solution I have tried Newton method, but it didn’t work well because of the above bullet point 2. I have also tried fitting 3 dimensional function

C#: Extract A Href Links from Html Text

// I know 1, 2, 3 is bad grouping name X( private readonly static Regex LINK_REGEX = new Regex( @"<a\s+[^>]*href\s*=\s*(?:(?<3>'|"")(?<1>[^\3>]*?)\3|(?<1>[^\s>]+))[^>]*>(?<2>.*?)</a>", RegexOptions.IgnoreCase | RegexOptions.Compiled ); public static void ExtractLinks(string text, ICollection<string> links) { LINK_REGEX.ApplyAllMatched(text, (m) => links.Add(m.Groups[1].Value)); } Helper method which processes matched string for each. public static void ApplyAllMatched(this Regex regex, string text, Action apply) { for (var m = regex.Match(text); m.Success; ) { apply(m); m = m.NextMatch(); } }