1. 程式人生 > >WPF控件保存為圖片Bitmap

WPF控件保存為圖片Bitmap

angle int transform window ima sta render style ant

        #region  控件保存為圖片--放大圖
        /// <summary>
        /// 控件保存為圖片--可處理放大圖
        /// </summary>
        /// <param name="element"></param>
        /// <returns></returns>
        public static Bitmap CreateNotRanderElementScreenshot(FrameworkElement element,  double MatrixTransform=1
) { var wantRanderSize = new System.Windows.Size(Math.Truncate(element.ActualWidth * MatrixTransform), Math.Truncate(element.ActualHeight * MatrixTransform)); element.Measure(wantRanderSize); element.SnapsToDevicePixels = true; element.Arrange(
new Rect(new System.Windows.Point(0, 0), wantRanderSize)); return CreateElementScreenshot(element, Convert.ToInt32(Math.Truncate(element.ActualWidth * MatrixTransform)), Convert.ToInt32(Math.Truncate(element.ActualHeight * MatrixTransform))); } private static Bitmap CreateElementScreenshot(Visual visual, int
sWidth, int sHeight) { RenderTargetBitmap bmp = new RenderTargetBitmap(sWidth, sHeight, 96, 96, PixelFormats.Default); //RenderTargetBitmap bmp = new RenderTargetBitmap((int)RenderSize.Width, (int)RenderSize.Height, 96, 96, PixelFormats.Default); bmp.Render(visual); Bitmap bitmap = new Bitmap(bmp.PixelWidth, bmp.PixelHeight, System.Drawing.Imaging.PixelFormat.Format32bppArgb); BitmapData data = bitmap.LockBits(new System.Drawing.Rectangle(System.Drawing.Point.Empty, bitmap.Size), ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb); bmp.CopyPixels(Int32Rect.Empty, data.Scan0, data.Height * data.Stride, data.Stride); bitmap.UnlockBits(data); //bitmap.Save(@"D:\TestCode\WpfDragMoveTest\SerialPortPrint\bin\Debug\5555.png", ImageFormat.Png); return bitmap; } #endregion

WPF控件保存為圖片Bitmap