协助imagestream进行图像控制wpf [英] assiging the imagestream to image control wpf
问题描述
早上好
在wpf中并使用C#.net,基本上我有了图像流,还有位图,我需要知道如何将位图分配给图像控件(或直接从图像流分配给图像控件).
我有一些示例代码,如
hi good morning
in wpf and using C#.net basically i had the imagestream, and also the bitmap , i need to know that how to assign the bitmap to image control (or) directly from imagestream to image control
i had some sample code like
byte[] ImageInBytes = MyWebClient.DownloadData(a.ToString());
// ''CREATE A MEMORY STREAM USING THE BYTES
System.IO.MemoryStream ImageStream = new System.IO.MemoryStream(ImageInBytes);
// ''CREATE A BITMAP FROM THE MEMORY STREAM
System.Drawing.Bitmap megatron = new System.Drawing.Bitmap(ImageStream);
推荐答案
Image.Source是System.Windows.Media.ImageSource,因此您需要将流解码为ImageSource派生的类对象.一旦有了该源,就可以将其分配给Image.Source属性.
几个例子...
Image.Source is a System.Windows.Media.ImageSource so you need to decode your stream into an ImageSource-derived class object. Once you have that source you can assign it to the Image.Source property.
A couple examples...
// Use a specific decoder
PngBitmapDecoder decoder = new PngBitmapDecoder(ImageStream, BitmapCreateOptions.None, BitmapCacheOption.OnLoad);
BitmapSource source = decoder.Frames[0];
image.Source = source;
// Let the framework pick a decoder
BitmapFrame source = BitmapFrame.Create(ImageStream, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad);
image.Source = source;
并摆脱System.Drawing.Bitmap ...那不是WPF:)
And get rid of the System.Drawing.Bitmap...that''s not WPF :)
这是一种将System.Drawing.Bitmap
转换为System.Windows.Media.Imaging.BitmapSource
的方法. />
请执行以下操作:
The is a way to convert to convertSystem.Drawing.Bitmap
intoSystem.Windows.Media.Imaging.BitmapSource
.
Do the following:
using System;
using System.Windows;
using BitmapSource = System.Windows.Media.Imaging.BitmapSource;
using InteropImaging = System.Windows.Interop.Imaging;
using System.Drawing.Imaging;
//...
[System.Runtime.InteropServices.DllImport("gdi32.dll")]
private static extern bool DeleteObject(IntPtr hObject);
public static BitmapSource CreateBitmapSourceFromBitmap(System.Drawing.Bitmap bitmap) {
IntPtr hBitmap = bitmap.GetHbitmap();
try {
return System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
hBitmap,
IntPtr.Zero,
Int32Rect.Empty,
System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());
} finally {
DeleteObject(hBitmap);
} //exception
} //CreateBitmapSourceFromBitmap
它创建内存流的另一种方法是,向其写入一个位图,然后从中读取.您可以通过使用一种系统进行书写并通过另一种系统进行阅读来进行转换.由于System.Windows.Interop.Imaging
已经完成了技巧,因此您可能不需要它.
(原则上,您可以忽略删除中间位图和资源泄漏的问题,以避免使用P/Invoke.如果仅执行固定次数(例如,一次或两次),则可以忽略泄漏.请小心. .)
Another way it to create a memory stream, write a bitmap to it and then read from it. You convert by writing using one system and reading by another one. As System.Windows.Interop.Imaging
already does the trick you may not need it.
(In principle, you can ignore the problem with deletion of the intermediate bitmap and the resource leak to avoid using P/Invoke. If you do it just fixed number of times (say, once or twice) you can ignore the leak. Be careful.)
这篇关于协助imagestream进行图像控制wpf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!