WPF/WinForms/GDI 互操作:将 WriteableBitmap 转换为 System.Drawing.Image?
如何将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
此博客 帖子 展示了如何对您的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:
I'd definitely prefer the first solution (the one described in the blog post).
如果您的位图数据未压缩,您可能可以使用此
System.Drawing.Bitmap
构造函数:位图(Int32、Int32、Int32、PixelFormat、IntPtr)。如果位图编码为 jpg 或 png,请从位图数据创建一个
MemoryStream
,并将其与 位图(流) 构造函数。编辑:
由于您要将位图发送到网络服务,我建议您首先对其进行编码。 System.Windows.Media.Imaging 命名空间中有多个编码器。例如:
在接收端,简单地说:
希望有所帮助。
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:
On the receiving end, simply:
Hope that helps.
问题是针对 WPF 的,
Pixels
似乎不是WriteableBitmap
的属性。这里的一些答案指向 SilverLight 文章,所以我怀疑这可能是 WPF 和 SilverLight 之间的差异。The question is for WPF, and
Pixels
does not appear to be a property ofWriteableBitmap
. Some of the answers here point to SilverLight articles, so I suspect this might be a difference between WPF and SilverLight.