- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在从内存流中保存图像时遇到了一些麻烦。
这是我的代码:
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
上我有以下异常:
A generic error occurred in 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>
最佳答案
根据 documentation :
You must keep the stream open for the lifetime of the Bitmap.
Bitmap 需要将其数据存储在同一位置,我推断(尽管没有证据证明这一点)Bitmap
不会复制数据,而是使用流并对其保持锁定。
此外,没有证据表明Clone
会使用字节表示的副本创建新的位图。你的测试用例表明它不是一个案例。
因此,恐怕您需要在图像的整个生命周期内保持流打开状态。它需要内存,没错,但如果 Bitmap 复制了数据,您仍然需要该内存来表示位图。因此,打开流不再消耗内存(如果我之前的推论是正确的)。
如果你真的想克服位图对原始内存流的依赖,你需要在新位图上绘制原始位图,而不是像 here 那样克隆。但这会影响性能,我最好重新分析是否保留原始流不是一个好主意,只是确保在处理 bitmap 时关闭它。
关于c# - 如何在 C# 中复制 Bitmap.Image,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8872767/
我是一名优秀的程序员,十分优秀!