如何在 C# 中复制 Bitmap.Image
我在从内存流保存图像时遇到一些麻烦。
这是我的代码:
MemoryStream ms = new MemoryStream(onimg);
if (ms.Length > 0)
{
Bitmap bm = new Bitmap(ms);
returnImage = (Image)bm.Clone();
}
ms.Close();
returnImage.Save(@"C:\img.jpeg");
在 returnImage.Save
上,我遇到以下异常:
GDI+ 中发生一般错误。
如果我不关闭 MemoryStream 一切都可以,但在一段时间后需要大量内存。
我该怎么做?
编辑:保存只是演示..我确实需要 returnImage 将其放置在 ObservableCollection 中,并在我需要将其转换为 System.Windows.Media.Imaging.BitmapImage() 时显示在窗口中;
[ValueConversion(typeof(System.Drawing.Image), typeof(System.Windows.Media.ImageSource))]
public class ImageConverter : IValueConverter
{
public object Convert(object value, Type targetType,
object parameter, CultureInfo culture)
{
// empty images are empty...
if (value == null) { return null; }
var image = (System.Drawing.Image)value;
// Winforms Image we want to get the WPF Image from...
var bitmap = new System.Windows.Media.Imaging.BitmapImage();
bitmap.BeginInit();
MemoryStream memoryStream = new MemoryStream();
// Save to a memory stream...
image.Save(memoryStream, ImageFormat.Bmp);
// Rewind the stream...
memoryStream.Seek(0, System.IO.SeekOrigin.Begin);
bitmap.StreamSource = memoryStream;
bitmap.EndInit();
return bitmap;
}
public object ConvertBack(object value, Type targetType,
object parameter, CultureInfo culture)
{
return null;
}
}
当我这样做时 XAML
<DataTemplate>
<Image Width="32" Height="32" Source="{ Binding Thumb, Converter={StaticResource imageConverter} }" />
</DataTemplate>
I have some troubles with saving a image from memorystream.
Here is my code:
MemoryStream ms = new MemoryStream(onimg);
if (ms.Length > 0)
{
Bitmap bm = new Bitmap(ms);
returnImage = (Image)bm.Clone();
}
ms.Close();
returnImage.Save(@"C:\img.jpeg");
And on returnImage.Save
i have the following exception:
A generic error occurred in GDI+.
If I do not close the MemoryStream
all is OK but that requires lot of memory after some time.
How can I do this?
EDIT:That Save is only demonstration.. I really need returnImage for place it in ObservableCollection and display in window when i Need it convert to System.Windows.Media.Imaging.BitmapImage();
[ValueConversion(typeof(System.Drawing.Image), typeof(System.Windows.Media.ImageSource))]
public class ImageConverter : IValueConverter
{
public object Convert(object value, Type targetType,
object parameter, CultureInfo culture)
{
// empty images are empty...
if (value == null) { return null; }
var image = (System.Drawing.Image)value;
// Winforms Image we want to get the WPF Image from...
var bitmap = new System.Windows.Media.Imaging.BitmapImage();
bitmap.BeginInit();
MemoryStream memoryStream = new MemoryStream();
// Save to a memory stream...
image.Save(memoryStream, ImageFormat.Bmp);
// Rewind the stream...
memoryStream.Seek(0, System.IO.SeekOrigin.Begin);
bitmap.StreamSource = memoryStream;
bitmap.EndInit();
return bitmap;
}
public object ConvertBack(object value, Type targetType,
object parameter, CultureInfo culture)
{
return null;
}
}
And XAML when i do this
<DataTemplate>
<Image Width="32" Height="32" Source="{ Binding Thumb, Converter={StaticResource imageConverter} }" />
</DataTemplate>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据文档:
Bitmap 需要将其数据存储在同一位置,并且我推断(尽管没有证据)Bitmap 不会复制数据,而是使用流并对其保持锁定。
此外,没有证据表明克隆将使用字节表示的副本创建新的位图。你的测试用例表明这不是一个案例。
因此,恐怕您需要在图像的生命周期内保持流开放。它需要内存,确实如此,但是如果位图复制了数据,您仍然需要该内存来表示位图。因此,开放流不会消耗更多内存(如果我之前的推论是正确的)。
如果您确实想克服位图对原始内存流的依赖,则需要在新位图上绘制原始位图,而不是像 此处。
但这会影响性能,如果保留原始流不是一个好主意,我最好重新分析,只需确保在处理位图时将其关闭即可。
According to documentation:
Bitmap needs to store its data samewhere, and I deduce (although have no proof for this) that
Bitmap
does not copy the data, instead using the stream, keeping a lock on it.Also, there is no evidence that
Clone
will create new bitmap with the copy of byte representation. And your testcase suggests it is not a case.Therefore, I am afraid you need to keep your stream open for the lifetime of your image. It requires memory, true, but otherwise if Bitmap copied the data, you will still need that memory for the bitmap representation. Therefore, no more memory is consumed with open stream (if my previous deduction was true).
If you really want to overcome the bitmap's dependency on the original memory stream, you will need to draw the orginal bitmap on the new one instead of cloning like here.
But this will impact the performance, I would better re-analyze if it is not a good idea to keep the original stream, just making sure it is closed when bitmap is disposed.
保存到磁盘基本上不是克隆吗?
Isn't a save to disk basically a clone?