ãWindows Phone Advent Calendar "ひとり" 2011ã第2æ¥ç®ã
UIElementãWriteableBitmapã«å¤æãã
UIè¦ç´ ã¯UIElementã¯ã©ã¹ãç¶æ¿ãã¦ãããWriteableBitmapã®ã³ã³ã¹ãã©ã¯ã¿å¼æ°ã«æå®ãããã¨ãåºæ¥ã¾ãã
public WriteableBitmap(
UIElement element,
Transform transform
)
|element|ããããããã«æç»ããUIè¦ç´ | |transform|ããããããã¸æç»åã®è¦ç´ ã®å¤å½¢ãæå®ãããããæå®|
transformã¯ãããããããã¸ã®æç»åã®æå¾ã®ã¹ãããã¨ãã¦è¦ç´ ã«å¤æãé©ç¨ããã¾ããnullãæå®ããå ´åã¯å¤æãè¡ãã¾ããã
以ä¸ã«ãããã ã§å®è¡ã§ãããµã³ãã«ã³ã¼ããç¨æãã¾ããããã¼ã¸ãWriteableBitmapã«å¤æããåé¢ã¹ãã¬ã¼ã¸ã«JPEGãã¼ã¿ã¨ãã¦ä¿åãã¦ãã¾ãã
// ãã¼ã¸ã®ãWriteableBitmapã«å¤æãã var bitmap = new WriteableBitmap(this, null); // åé¢ã¹ãã¬ã¼ã¸ã«ãã¼ã¸ã®ç»åãæç» using (var store = IsolatedStorageFile.GetUserStoreForApplication()) using (var strm = store.CreateFile("page.jpg")) { bitmap.SaveJpeg(strm, bitmap.PixelWidth, bitmap.PixelHeight, 0, 80); }
åå½¢ã°ã©ãã¼ã·ã§ã³ã®åè§å½¢ãWriteableBitmapã«å¤æãã
ä¸å³ã®ãããªåå½¢ã°ã©ãã¼ã·ã§ã³ã®Rectangleã³ã³ããã¼ã«ãWriteableBitmapã«å¤æãããã¨èãã¦ãã¾ãã
ã³ã¼ãã§æ¸ãã¦ãè¯ãã®ã§ãããå®è£ ã®é£æ度ãé«ããã¨ã¨é度ãåºãªããã¨ãäºæ³ã§ãã¾ããã®ã§ãåè¨ããUIElementããWriteableBitmapã¸å¤æããæ¹æ³ãä¸æãå©ç¨ãã¦ãã¿ã¾ãããã
XAMLã§åå½¢ã°ã©ãã¼ã·ã§ã³ãæ¸ããå ´å
å ã»ã©ã®ç»åã¯XAMLã§æ¸ãã¨ä»¥ä¸ã®éãã§ããXAMLã ã¨ãã®ãããã·ã³ãã«ã§ãã
<Rectangle StrokeThickness="1"> <Rectangle.Fill> <RadialGradientBrush> <GradientStop Color="Yellow" Offset="0"/> <GradientStop Color="Black" Offset="1"/> </RadialGradientBrush> </Rectangle.Fill> </Rectangle>
ããã°ã©ã ã§åå½¢ã°ã©ãã¼ã·ã§ã³ãæ¸ããå ´å
åè§å½¢è¦ç´ ã®ã¤ã³ã¹ã¿ã³ã¹ã®ä½æããå¾ã«ãã©ããªã°ã©ãã¼ã·ã§ã³ã«ãããããGradientStopsããããã£ã«æå®ããã®ã¡ã«ãåè§å½¢è¦ç´ ãã³ã³ã¹ãã©ã¯ã¿å¼æ°ã«WriteableBitmapã¤ã³ã¹ã¿ã³ã¹ãçæãã¾ãã
WriteableBitmapã¤ã³ã¹ã¿ã³ã¹ã«ã¯SaveJpegã¡ã½ãããåå¨ãã¦ããã®ã§ãJPEGç»åã«ã¨ã³ã³ã¼ãå¾ã«åé¢ã¹ãã¬ã¼ã¸ã¸ã®ä¿åãç°¡åã«ã§ãã¾ãããã°ã©ãã¼ã·ã§ã³ãããç»åã®ãã¯ã»ã«æä½ãããããã¨ãåºæ¥ãããã«ãªãã¾ãã
// åè§å½¢è¦ç´ ã®ã¤ã³ã¹ã¿ã³ã¹ã®ä½æ var rect = new Rectangle() { Width = 404, Height = 571 }; // ã°ã©ãã¼ã·ã§ã³ç»åã®ä½æ var brush = new RadialGradientBrush(); brush.GradientStops.Add(new GradientStop() { Color = Colors.Yellow, Offset = 0 }); brush.GradientStops.Add(new GradientStop() { Color = Colors.Black, Offset = 1 }); rect.Fill = brush; // ç»åã®ãã¯ãåããã«ããã®ã§å¤æ ã®ç·ãå¼ã rect.Stroke = new SolidColorBrush() { Color = Colors.Purple }; // åé¢ã¹ãã¬ã¼ã¸ã«ã°ã©ãã¼ã·ã§ã³ç»åãæç» var bmp = new WriteableBitmap(rect, null); using (var store = IsolatedStorageFile.GetUserStoreForApplication()) using (var strm = store.CreateFile("rectGradation.jpg")) { bmp.SaveJpeg(strm, 404, 571, 0, 80); } // ç»åã¨ãã¦Imageè¦ç´ ã®ã½ã¼ã¹ã«è¨å® image.Source = bmp;
åçãªã°ã©ãã¼ã·ã§ã³ãç¨æããã®ã¯å°ãå³ããããããã¾ããããåºå®ã®ã°ã©ãã¼ã·ã§ã³ã使ç¨ããã®ã§ããã°ç°¡åã«ä½¿ããã¨æãã¾ãããã ãWriteableBitampã使ç¨ãã¦ããé¢ä¿ã§UIã¹ã¬ããä¸ã§ããå¦çãåºæ¥ã¾ããã
ããã«é¢ãã¦ã¯éåæå¦çåã¯é£ãããã§ããã
Shape以å¤ã®è¦ç´ ãWriteableBitmapã¸å¤æãã
å ã»ã©UIElementããWriteableBitmap(UIElement, Transform)ã使ã£ã¦WriteableBitmapã¸å¤æãè¡ãã¾ããããããã¹ãã¬ã¼ãã«å¤æå¯è½ãªã®ã¯ä¸é¨ã®è¦ç´ ã ãã§ãã
ä¾ãã°Gridãå¤æãããã¨ããã¨ãã¨ã©ã¼ã³ã¼ãã ãã®çç±ãåããã«ããExceptionãçºçãã¦ãã¾ãã¾ãã
ä½æ ãï¼çç±ã¯åç´ã§ãµã¤ãºãåãããªãã®ã§ãããããããä½ããªãããã§ããä¸è¨ã®ã³ã¼ããå®è¡ããã¨WriteableBitmapã®ã¤ã³ã¹ã¿ã³ã¹çææã«Exceptionãçºçãã¾ãã
var grid = new Grid() { Width = 456, Height = 624 }; var size = new Size(grid.Width, grid.Height); var bmp = new WriteableBitmap(grid, null); // ããã§è½ã¡ã
Rectangleã代表ã¨ããShapeã®å ´åã¯ãæç»æã®ãµã¤ãºRectangle.ActualHeightã¨Rectangle.ActualWidthã®å¤ã決å®ãã¦ãããããåé¡ãªãWriteableBitmapã«å¤æãå¯è½ã§ãã
ããããGridã®å ´åã¯æç»æã«åãã¦æ±ºå®ããã®ã§WriteableBitmapã¸ã®å¤æãä¸å¯è½ã§ããéã«è¨ãã°UIä¸ã«æç»ããªãã¾ã§ãåæ§ã®ããã»ã¹ãè¸ããã¨ã«ãã£ã¦ãActualHeightãActualWidthããããã£ãæ´æ°ããã¦WriteableBitmapã¸ã®å¤æãå¯è½ã«ãªãã¨ãããã¨ã§ãã
å ã»ã©ã®ã³ã¼ãã«2è¡è¿½å ããã ãã§ãããããã§åé¡ãªãWriteableBitmapã¸å¤æãããã¨ãåºæ¥ãããã«ãªã£ã¦ããã¯ãã§ãã
var grid = new Grid() { Width = 456, Height = 624 }; var size = new Size(grid.Width, grid.Height); grid.Arrange(new Rect(new Point(0, 0), size)); grid.UpdateLayout(); var bmp = new WriteableBitmap(grid, null);