c# - 是否有可能获得对 system.drawing.graphics 类的原始图像缓冲区的引用?

标签 c# wpf image

我正在尝试将图像从非托管控件渲染到 WPF 窗口。到目前为止,我能够得到一个工作图形对象(因为我能够将图像覆盖到非托管控件)。我希望做的是相反的事情,即从图形对象中捕获图像并保存为另一个控件的图像源。

var graphics = Graphics.FromHwnd(hwndPtr);//From image unmanaged source
graphics.(??) // save to bitmap or any image format

如果不能直接用图形对象保存图像,是否可以获取图形对象图像缓冲区的原始引用? (与下面的代码一起使用)

var bmp = (InteropBitmap)Imaging.CreateBitmapSourceFromMemorySection(
                                  hwntPtr,120,120,format,stride,0);

提前致谢..

最佳答案

这是从 MSDN 论坛文章中稍微修改的代码,但就从 System.Drawing.Graphics 对象获取 System.Drawing.Bitmap 而言,它应该可以解决问题。

[DllImport("coredll.dll", SetLastError = true)]

internal static extern bool BitBlt(IntPtr hDest, int nDstX, int nDstY, int nWidth, int nHeight, IntPtr hSrc, int nSrcX, int nSrcY, uint BitBltOperation);

Bitmap SaveAsBitmap(Graphics gxSrc, int sizeX, int SizeY)
{
    Bitmap bm = new Bitmap(sizeX, sizeY);
    Graphics gxDst = Graphics.FromImage(bm);
    IntPtr hSrc = gxSrc.GetHdc();
    IntPtr hDst = gxDst.GetHdc();
    BitBlt(hDst, 0, 0, sizeX, sizeY, hSrc, 0, 0, 0xcc0020); // SourceCopy operation
    gxDst.ReleaseHdc(hDst);
    gxSrc.ReleaseHdc(hSrc);
    gxDst.Dispose();
    return bm;
}

Original article

关于c# - 是否有可能获得对 system.drawing.graphics 类的原始图像缓冲区的引用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10112018/

相关文章:

wpf - 如果 Prism 模块崩溃会怎样?应用还稳定吗?

带水印的PHP图片上传

PHP和MYSQL显示多个名称的1条记录

c# - 使用 LINQ 从不同类获取唯一列表

c# - Web 服务中的数据库访问

c# - 多对多和 Linq : updating relationships

c# - 如何在运行时使用 Entity Framework 在sql数据库中创建表并在运行时更新模型?

.net - 当 TextBox 为空时,数据绑定(bind)不会更新

c# - 你如何制作一个完全方形的按钮?

java - 段落之间的图像使用 iText