繁体   English   中英

将位图从列表保存到文件时出现 C# GDI+ 错误

[英]C# GDI+ error when saving Bitmap from List to File

我目前正在从 sql 数据库中提取 VARBINARY 数据,将其转换为 BITMAP,然后将其中的每一个添加到 List 对象中。

然而,当我尝试保存上面列表中的每个图像时,我在 image.save 函数上得到一个通用的 GDI+ 异常。

我试过的:
- 更改路径(路径当前为 C:\\Temp 用于测试)
- 检查权限(路径具有完全权限)
- 从 BITMAP 列表更改为 IMAGE 列表 - 更改保存函数的 ImageFormat - 设置保存函数的 EncodingQuality

在正确的目录中创建了一个具有正确名称的 0KB 图像,因此不太确定是什么导致了问题。

以下是我的保存代码:

public void doImport(List<Bitmap> images, string certNo, string saveLoc, ListView.ListViewItemCollection items) {
        string path = String.Format("{0}\\{1}", saveLoc, certNo);

        if ( !Directory.Exists(path) ) { Directory.CreateDirectory(path); }

        for ( int i = 0; i < items.Count; i++ ) {
            Bitmap image = images[i];
            string imgPath = String.Format("{0}\\{1}\\{1}-{2}.jpg", saveLoc, certNo, items[i].Text);

            try {
                image.Save(imgPath, ImageFormat.Jpeg);
                if ( File.Exists(imgPath) ) { images.Remove(image); }
            } catch (Exception ex) {
                MessageBox.Show(String.Format("Warning: Could not save image: {0}, it has been skipped.\n\n{1}", items[i].Text, ex.Message), 
                    "Warning", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }
    }

谢谢

阅读几个类似的问题后简单修复。 刚刚改变了这一行:

for ( int i = 0; i < items.Count; i++ ) {
        Bitmap image = images[i];

到这一行:

for ( int i = 0; i < items.Count; i++ ) {
        Bitmap image = new Bitmap(images[i]);

谢谢 :)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM