WPF/WinForms/GDI 互操作:将 WriteableBitmap 转换为 System.Drawing.Image?

发布于 2024-09-09 12:01:32 字数 644 浏览 4 评论 0原文

如何将 WPF WriteableBitmap 对象转换为 System.Drawing.Image?

我的 WPF 客户端应用程序将位图数据发送到 Web 服务,并且 Web 服务需要在该端构造一个 System.Drawing.Image。

我知道我可以获取 WriteableBitmap 的数据,将信息发送到 Web 服务:

// WPF side:

WriteableBitmap bitmap = ...;
int width = bitmap.PixelWidth;
int height = bitmap.PixelHeight;
int[] pixels = bitmap.Pixels;

myWebService.CreateBitmap(width, height, pixels);

但在 Web 服务端,我不知道如何从这些数据创建 System.Drawing.Image。

// Web service side:

public void CreateBitmap(int[] wpfBitmapPixels, int width, int height)
{
   System.Drawing.Bitmap bitmap = ? // How can I create this?
}

How can I convert a WPF WriteableBitmap object to a System.Drawing.Image?

My WPF client app sends bitmap data to a web service, and the web service needs to construct a System.Drawing.Image on that end.

I know I can get the data of a WriteableBitmap, send the info over to the web service:

// WPF side:

WriteableBitmap bitmap = ...;
int width = bitmap.PixelWidth;
int height = bitmap.PixelHeight;
int[] pixels = bitmap.Pixels;

myWebService.CreateBitmap(width, height, pixels);

But on the web service end, I don't know how to create a System.Drawing.Image from this data.

// Web service side:

public void CreateBitmap(int[] wpfBitmapPixels, int width, int height)
{
   System.Drawing.Bitmap bitmap = ? // How can I create this?
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

萌酱 2024-09-16 12:01:32

此博客 帖子 展示了如何对您的WriteableBitmap 作为 jpeg 图像。也许这有帮助?

如果您确实想传输原始图像数据(像素),您可以:

  1. 创建一个 System.Drawing.Bitmap 使用正确的大小
  2. 迭代原始数据,将原始数据转换为 System.Drawing.Color(例如通过 Color.FromArgb() 并通过 SetPixel()

我绝对更喜欢第一个解决方案(所描述的那个在博客文章中)。

this blog post shows how to encode your WriteableBitmap as a jpeg image. Perhaps that helps?

If you really want to transfer the raw image data (pixels) you could:

  1. create a System.Drawing.Bitmap with the correct size
  2. iterate over your raw data, convert the raw data to a System.Drawing.Color (e.g. via Color.FromArgb() and set each pixel color in the newly created image via SetPixel()

I'd definitely prefer the first solution (the one described in the blog post).

醉南桥 2024-09-16 12:01:32

如果您的位图数据未压缩,您可能可以使用此 System.Drawing.Bitmap 构造函数:位图(Int32、Int32、Int32、PixelFormat、IntPtr)

如果位图编码为 jpg 或 png,请从位图数据创建一个 MemoryStream,并将其与 位图(流) 构造函数。

编辑:

由于您要将位图发送到网络服务,我建议您首先对其进行编码。 System.Windows.Media.Imaging 命名空间中有多个编码器。例如:

    WriteableBitmap bitmap = ...;
    var stream = new MemoryStream();               
    var encoder = new JpegBitmapEncoder(); 
    encoder.Frames.Add( BitmapFrame.Create( bitmap ) ); 
    encoder.Save( stream ); 
    byte[] buffer = stream.GetBuffer(); 
    // Send the buffer to the web service   

在接收端,简单地说:

    var bitmap = new System.Drawing.Bitmap( new MemoryStream( buffer ) );

希望有所帮助。

If your bitmap data is uncompressed, you could probably use this System.Drawing.Bitmap constructor: Bitmap(Int32, Int32, Int32, PixelFormat, IntPtr).

If the bitmap is encoded as jpg or png, create a MemoryStream from the bitmap data, and use it with the Bitmap(Stream) constructor.

EDIT:

Since you're sending the bitmap to a web service, I suggest you encode it to begin with. There are several encoders in the System.Windows.Media.Imaging namespace. For example:

    WriteableBitmap bitmap = ...;
    var stream = new MemoryStream();               
    var encoder = new JpegBitmapEncoder(); 
    encoder.Frames.Add( BitmapFrame.Create( bitmap ) ); 
    encoder.Save( stream ); 
    byte[] buffer = stream.GetBuffer(); 
    // Send the buffer to the web service   

On the receiving end, simply:

    var bitmap = new System.Drawing.Bitmap( new MemoryStream( buffer ) );

Hope that helps.

冰魂雪魄 2024-09-16 12:01:32

问题是针对 WPF 的,Pixels 似乎不是 WriteableBitmap 的属性。这里的一些答案指向 SilverLight 文章,所以我怀疑这可能是 WPF 和 SilverLight 之间的差异。

The question is for WPF, and Pixels does not appear to be a property of WriteableBitmap. Some of the answers here point to SilverLight articles, so I suspect this might be a difference between WPF and SilverLight.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文