WPF/WinForms/GDI互操作:将WriteableBitmap转换为System.Drawing.Image? [英] WPF/WinForms/GDI interop: converting a WriteableBitmap to a System.Drawing.Image?

查看:239
本文介绍了WPF/WinForms/GDI互操作:将WriteableBitmap转换为System.Drawing.Image?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我的WPF客户端应用程序将位图数据发送到Web服务,并且该Web服务需要在那一端构造一个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.

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

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);

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

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?
}

推荐答案

此博客帖子显示了如何将WriteableBitmap编码为jpeg图像.也许有帮助吗?

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. 创建 System.Drawing.Bitmap 尺寸正确
  2. 遍历您的原始数据,将原始数据转换为System.Drawing.Color(例如,通过
  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).

这篇关于WPF/WinForms/GDI互操作:将WriteableBitmap转换为System.Drawing.Image?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆