-
[C#]Canvas to BitmapImageC#/WPF_예제소스 2022. 5. 24. 11:34
public static class SBA { public static WriteableBitmap SaveAsWriteableBitmap(Canvas surface) { if (surface == null) return null; // Save current canvas transform Transform transform = surface.LayoutTransform; // reset current transform (in case it is scaled or rotated) surface.LayoutTransform = null; // Get the size of canvas Size size = new Size(surface.ActualWidth, surface.ActualHeight); // Measure and arrange the surface // VERY IMPORTANT surface.Measure(size); surface.Arrange(new Rect(size)); // Get the size of canvas size = new Size(surface.ActualWidth, surface.ActualHeight); // Measure and arrange the surface // VERY IMPORTANT surface.Measure(size); surface.Arrange(new Rect(size)); // Create a render bitmap and push the surface to it RenderTargetBitmap renderBitmap = new RenderTargetBitmap( (int)size.Width, (int)size.Height, 96d, 96d, PixelFormats.Pbgra32); renderBitmap.Render(surface); //Restore previously saved layout surface.LayoutTransform = transform; //create and return a new WriteableBitmap using the RenderTargetBitmap return new WriteableBitmap(renderBitmap); } public static BitmapImage WriteableBitmapToBitmapImage(WriteableBitmap wbm) { BitmapImage bmImage = new BitmapImage(); using (MemoryStream stream = new MemoryStream()) { PngBitmapEncoder encoder = new PngBitmapEncoder(); encoder.Frames.Add(BitmapFrame.Create(wbm)); encoder.Save(stream); bmImage.BeginInit(); bmImage.CacheOption = BitmapCacheOption.OnLoad; bmImage.StreamSource = stream; bmImage.EndInit(); bmImage.Freeze(); } return bmImage; } public static BitmapImage CanvasToBitmap(Canvas c) { return WriteableBitmapToBitmapImage(SaveAsWriteableBitmap(c)); } }
https://stackoverflow.com/questions/19098089/convert-canvas-to-bitmapimage-crop-to-path
Convert Canvas to BitMapImage & crop to path
I have a UserControl in a windows phone 8 app, where the user draws on this Usercontrol. I would like to convert this to an image, an example could be bitmap. I have found this "converting a canvas
stackoverflow.com
728x90'C# > WPF_예제소스' 카테고리의 다른 글
[WPF] OnPropertyChanged (0) 2022.07.01 [WPF]모눈종이 그리기 (0) 2022.06.29 [C#] 열화상 이미지에 온도데이터 표출 (0) 2022.04.26 [C#] Redirect WriteLine stream to a textblock (0) 2022.04.12 [C#]ColorMap (0) 2022.04.06 댓글