8,734
社区成员
发帖
与我相关
我的任务
分享
DrawingVisual drawingVisual = new DrawingVisual();
DrawingContext drawingContext = drawingVisual.RenderOpen();
drawingContext.DrawLine(pen, new Point(0,0), new Point(1023, 200));
drawingContext.DrawLine(pen, new Point(1023,0), new Point(1023, 1023));
drawingContext.Close();
RenderTargetBitmap bmp;
bmp = new RenderTargetBitmap(1024, 1024, 120, 96, PixelFormats.Pbgra32);
bmp.Render(drawingVisual);
bmp = new RenderTargetBitmap(2048 1024, 120, 96, PixelFormats.Pbgra32);
using Microsoft.Win32.SafeHandles;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Security;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;
namespace WDataViewer
{
//RenderTargetBitmap把视觉树中的一部分光栅化以位图的形式保存,你可以利用下面的这个类以位图的形式呈现视觉元素,当然得考虑系统DPI设置及视觉元素的转换。(感谢Adam Smith在反转换这方面的建议)
public class VisualUtility
{
public static BitmapSource CreateBitmapFromVisual(Double width,
Double height,
Visual visualToRender,
Boolean undoTransformation)
{
if (visualToRender == null)
{
return null;
}
// The PixelsPerInch()方法用于读取屏幕上DPI的设置
//如果你想以特定的分辨率创建一个位图,你可以直接把某一dpiX或dpiY传参给RenderTargetBitmap构造器。
RenderTargetBitmap bmp = new RenderTargetBitmap((Int32)Math.Ceiling(width),
(Int32)Math.Ceiling(height),
(Double)DeviceHelper.PixelsPerInch(Orientation.Horizontal),
(Double)DeviceHelper.PixelsPerInch(Orientation.Vertical),
PixelFormats.Pbgra32);
//如果我们想反转换,我们可以使用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);
}
}
}