WPF怎樣使用RenderTargetBitmap

WPF怎樣使用RenderTargetBitmap

RenderTargetBitmap把視覺樹中的一部分光柵化以位圖的形式保存,你可以利用下面的這個類以位圖的形式呈現(xiàn)視覺元素,當(dāng)然得考慮系統(tǒng)DPI設(shè)置及視覺元素的轉(zhuǎn)換。(感謝Adam Smith在反轉(zhuǎn)換這方面的建議) public class VisualUtility { public static BitmapSource CreateBitmapFromVisual(Double width, Double height, Visual visualToRender, Boolean undoTransformation) { if (visualToRender == null) { return null; } // The PixelsPerInch()方法用于讀取屏幕上DPI的設(shè)置 //如果你想以特定的分辨率創(chuàng)建一個位圖,你可以直接把某一dpiX或dpiY傳參給RenderTargetBitmap構(gòu)造器。

RenderTargetBitmap bmp = new RenderTargetBitmap((Int32)Math.Ceiling(width), (Int32)Math.Ceiling(height), (Double)DeviceHelper.PixelsPerInch(Orientation.Horizontal), (Double)DeviceHelper.PixelsPerInch(Orientation.Vertical), PixelFormats.Pbgra32); //如果我們想反轉(zhuǎn)換,我們可以使用VisualBrush if (undoTransformation) { DrawingVisual dv = new DrawingVisual(); using (DrawingContext dc = dv.RenderOpen()) { VisualBrush vb = new VisualBrush(visualToRender); dc.DrawRectangle(vb, null, new Rect(new Point(), new Size(width, height))); } bmp.Render(dv); } else { bmp.Render(visualToRender); } return bmp; } } internal class DeviceHelper { public static Int32 PixelsPerInch(Orientation orientation) { Int32 capIndex = (orientation == Orientation.Horizontal) ? 0x58 : 90; using (DCSafeHandle handle = UnsafeNativeMethods.CreateDC(\”DISPLAY\”)) { return (handle.IsInvalid ? 0x60 : UnsafeNativeMethods.GetDeviceCaps(handle, capIndex)); } } } internal sealed class DCSafeHandle : SafeHandleZeroOrMinusOneIsInvalid { private DCSafeHandle() : base(true) { } protected override Boolean ReleaseHandle() { return UnsafeNativeMethods.DeleteDC(base.handle); } } [SuppressUnmanagedCodeSecurity] internal static class UnsafeNativeMethods { [DllImport(\”gdi32.dll\”, CharSet = CharSet.Auto, ExactSpelling = true)] public static extern Boolean DeleteDC(IntPtr hDC); [DllImport(\”gdi32.dll\”, CharSet = CharSet.Auto, ExactSpelling = true)] public static extern Int32 GetDeviceCaps(DCSafeHandle hDC, Int32 nIndex); [DllImport(\”gdi32.dll\”, EntryPoint = \”CreateDC\”, CharSet = CharSet.Auto)] public static extern DCSafeHandle IntCreateDC(String lpszDriver, String lpszDeviceName, String lpszOutput, IntPtr devMode); public static DCSafeHandle CreateDC(String lpszDriver) { return UnsafeNativeMethods.IntCreateDC(lpszDriver, null, null, IntPtr.Zero); } }之所以你需要反轉(zhuǎn)換是因?yàn)槿绻阈枰鈻呕蒖enderTargetBitmap的視覺目標(biāo)元素已經(jīng)是轉(zhuǎn)換過的話(比如旋轉(zhuǎn),比例縮放,或是平移之類),這些效果會對最終產(chǎn)生位圖有影響,這可能不是你想要的結(jié)果。

“反轉(zhuǎn)換”參數(shù)不僅使你能夠?qū)崿F(xiàn)反轉(zhuǎn)換,而且可以得到原始的未經(jīng)過轉(zhuǎn)換的視覺元素呈現(xiàn)。在利用RenderTargetBitmap時的一些限制你也應(yīng)該清楚:首先,RenderTargetBitmap不會利用硬件加速,位圖是完全在內(nèi)存中產(chǎn)生的,并且整個過程也是在UI線程中實(shí)現(xiàn)的。其次,字體的顯示會顯示鋸齒效果,而不是清晰的呈現(xiàn)線條樣的效果。

wpf中怎么獲取image上的圖并轉(zhuǎn)化為bitmap

1.?

WPF中BitmapImage對象切割

在WPF中可以使用Int32Rect來切割圖片,當(dāng)然你要轉(zhuǎn)換也可以,剛剛回復(fù)了你的評論,寫得很簡略,這里給你一個轉(zhuǎn)換的方法吧:Bitmap BitmapSourceToBitmap(BitmapSource source) { System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(source.PixelWidth, source.PixelHeight, System.Drawing.Imaging.PixelFormat.Format32bppPArgb); BitmapData bitmapData = bitmap.LockBits(new System.Drawing.Rectangle(System.Drawing.Point.Empty, bitmap.Size), System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format32bppPArgb); source.CopyPixels(Int32Rect.Empty, bitmapData.Scan0, bitmapData.Height * bitmapData.Stride, bitmapData.Stride); bitmap.UnlockBits(bitmapData); return bitmap; }希望對你有幫助,有疑問請追問或是Hi

幾種常用WPF圖像處理方法介紹

WPF圖像處理在試駕開發(fā)中是非常有用的一個工具。開發(fā)人員可以通過WPF圖像處理簡單的實(shí)現(xiàn)精美的圖形界面顯示功能。

這里就為大家簡單介紹一下。

WPF自定義窗口相關(guān)方法介紹WPF邏輯樹具體概念詳解WPF控件相對位置解析WPF API特點(diǎn)總結(jié)WPF圖像格式如何轉(zhuǎn)換常用的WPF圖像處理包括縮放、裁切和旋轉(zhuǎn)等,如下是一個將圖像旋轉(zhuǎn)90度的例子。var imageStreamSource =File.OpenRead(@\”r:\\1\\24.bmp\”); var decoder = BitmapDecoder百科.Create(imageStreamSource, BitmapCreateOptions.PreservePixelFormat,BitmapCacheOption.Default); var bitmapFrame = decoder.Frames[0]; TransformedBitmap myRotatedBitmapSource = new TransformedBitmap(); myRotatedBitmapSource.BeginInit(); myRotatedBitmapSource.Source =bitmapFrame; // 旋轉(zhuǎn)90度 myRotatedBitmapSource.Transform =new RotateTransform(90); myRotatedBitmapSource.EndInit(); //旋轉(zhuǎn) var rotate = new RotateTransform(90); var rotatedBitMap = new TransformedBitmap(bitmapFrame, rotate); image1.Source = rotatedBitMap; ////裁剪 //CroppedBitmap chainedBitMap =new CroppedBitmap(bitmapFrame,new Int32Rect(100, 0, (int)bitmapFrame.Width – 100, (int)bitmapFrame.Height)); ////縮放 //var scare = new ScaleTransform(1.5, 2); //var scaredBitMap = new TransformedBitmap(bitmapFrame, scare); var encoder = new JpegBitmapEncoder(); encoder.Frames.Add(BitmapFrame.Create(rotatedBitMap)); //encoder.Frames.Add(BitmapFrame.Create(scaredBitMap)); //encoder.Frames.Add(BitmapFrame.Create(chainedBitMap)); encoder.Save(File.Create(@\”r:\\1\\3.jpg\”)); 和上面的WPF圖像處理例子相比,這里就是多了一個TransformedBitmap變換,其實(shí)這和xaml中的變換時一樣的。< Image Width=\”150\” Margin=\”5\”/sampleImages/watermelon.jpg\” > < TransformedBitmap.Transform> < RotateTransform Angle=\”90\”/> < /TransformedBitmap.Transform> < /TransformedBitmap> < /Image.Source> < /Image> 其它變換也都可以參照xaml中WPF圖像處理方式進(jìn)行,這里就不過多介紹了。