【问题标题】:Image Converter not working图像转换器不工作
【发布时间】:2017-07-24 20:23:24
【问题描述】:

谁能解释我为什么会收到错误:

GDI+ 中发生一般错误

代码如下:

[ValueConversion(typeof(System.Drawing.Image), typeof(ImageSource))]
public class ImageConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (value == null) return null;

        System.Drawing.Image img = (System.Drawing.Image)value;
        BitmapImage bitmap = new BitmapImage();

        using (MemoryStream ms = new MemoryStream())
        {
            img.Save(ms, ImageFormat.Bmp);
            ms.Seek(0, SeekOrigin.Begin);

            bitmap.BeginInit();
            bitmap.StreamSource = ms;
            bitmap.EndInit();
        }
        return bitmap;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

它在img.Save(ms, ImageFormat.Bmp) 行中断。

谢谢。

【问题讨论】:

  • 不是保存到流中,而是保存到文件中。这仍然会引发相同的错误吗?如果没有,您可以使用图像查看器打开该文件吗?

标签: c# bitmapimage ivalueconverter


【解决方案1】:

如果Save 方法失败,您应该查看您传递给该方法的源图像。你如何加载/创建这个图像?也许图像已经被释放或者它的内部流被关闭了?

查看此问题及其接受的答案:Image.Save(..) throws a GDI+ exception because the memory stream is closed

【讨论】:

    猜你喜欢
    • 2017-01-17
    • 2018-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-07
    • 2012-10-05
    • 1970-01-01
    相关资源
    最近更新 更多