2016-01-21 69 views
0

嘗試保存圖像時遇到GDI錯誤。我看過很多解決方案,似乎沒有人回答這個問題。我創建了一個簡單的加載和保存按鈕的窗體。在GDI中發生了一般性錯誤+簡單讀取並保存

加載使用流讀取,我正在保存到一個新的文件名。測試創建工作不是權限。文件abctest.jpg用0字節創建。

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.IO; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 

namespace CheckImageInUse 
{ 
    public partial class Form1 : Form 
    { 
    Bitmap originalImage = null; 

    public Form1() 
    { 
     InitializeComponent(); 
    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
     using (var fs = File.OpenRead(@"c:\images\DSC_0005.JPG")) 
     { 
      this.originalImage = new Bitmap(fs); 
     } 
    } 

    private void button2_Click(object sender, EventArgs e) 
    { 
     using (File.Create(@"c:\images\testcreate.txt")) 
     { } 

     this.originalImage.Save(@"c:\images\abctest.jpg"); 
    } 
    } 
} 

回答

0

我已經玩過了,唯一能看到的方式是讓這個工作與下面的負載變化一致。

private void button1_Click(object sender, EventArgs e) 
    { 
     using (var fs = File.OpenRead(@"c:\images\DSC_0005.JPG"))    
     using (Bitmap tempBitmap = new Bitmap(fs)) 
     { 
      this.originalImage = new Bitmap(tempBitmap); 
     }    
    } 

我認爲這是關於鏈接到磁盤上的原始圖像和一些內部指針。在內存中創建圖像的新副本允許我保存它。有點浪費,但工作。

相關問題