2011-04-12 68 views
1

我正在做OCR應用程序。當我運行系統將系統將picturebox3.image保存到文件夾中時,出現此錯誤。c#Bitmap.Save在GDI + Windows應用程序中發生一般性錯誤

//When user is selecting, RegionSelect = true 
    private bool RegionSelect = false; 
    private int x0, x1, y0, y1; 
    private Bitmap bmpImage; 

private void loadImageBT_Click(object sender, EventArgs e) 
{ 
    try 
    { 
     OpenFileDialog open = new OpenFileDialog(); 
     open.InitialDirectory = @"C:\Users\Shen\Desktop"; 

     open.Filter = "Image Files(*.jpg; *.jpeg)|*.jpg; *.jpeg"; 

     if (open.ShowDialog() == DialogResult.OK) 
     { 
      singleFileInfo = new FileInfo(open.FileName); 
      string dirName = System.IO.Path.GetDirectoryName(open.FileName); 
      loadTB.Text = open.FileName; 
      pictureBox1.Image = new Bitmap(open.FileName); 
      bmpImage = new Bitmap(pictureBox1.Image); 
     } 

    } 
    catch (Exception) 
    { 
     throw new ApplicationException("Failed loading image"); 
    } 
} 

//User image selection Start Point 
private void pictureBox1_MouseDown(object sender, MouseEventArgs e) 
{ 
    RegionSelect = true; 

    //Save the start point. 
    x0 = e.X; 
    y0 = e.Y; 
} 

//User select image progress 
private void pictureBox1_MouseMove(object sender, MouseEventArgs e) 
{ 
    //Do nothing it we're not selecting an area. 
    if (!RegionSelect) return; 

    //Save the new point. 
    x1 = e.X; 
    y1 = e.Y; 

    //Make a Bitmap to display the selection rectangle. 
    Bitmap bm = new Bitmap(bmpImage); 


    //Draw the rectangle in the image. 
    using (Graphics g = Graphics.FromImage(bm)) 
    { 
     g.DrawRectangle(Pens.Red, Math.Min(x0, x1), Math.Min(y0, y1), Math.Abs(x1 - x0), Math.Abs(y1 - y0)); 
    } 

    //Temporary display the image. 
    pictureBox1.Image = bm; 
} 

//Image Selection End Point 
private void pictureBox1_MouseUp(object sender, MouseEventArgs e) 
{ 
    // Do nothing it we're not selecting an area. 
    if (!RegionSelect) return; 
    RegionSelect = false; 

    //Display the original image. 
    pictureBox1.Image = bmpImage; 

    // Copy the selected part of the image. 
    int wid = Math.Abs(x0 - x1); 
    int hgt = Math.Abs(y0 - y1); 
    if ((wid < 1) || (hgt < 1)) return; 

    Bitmap area = new Bitmap(wid, hgt); 
    using (Graphics g = Graphics.FromImage(area)) 
    { 
     Rectangle source_rectangle = new Rectangle(Math.Min(x0, x1), Math.Min(y0, y1), wid, hgt); 
     Rectangle dest_rectangle = new Rectangle(0, 0, wid, hgt); 
     g.DrawImage(bmpImage, dest_rectangle, source_rectangle, GraphicsUnit.Pixel); 
    } 

    // Display the result. 
    pictureBox3.Image = area; 


    ** ERROR occuer here!!!!!** 
    area.Save(@"C:\Users\Shen\Desktop\LenzOCR\TempFolder\tempPic.jpg"); // error line occcur 

    singleFileInfo = new FileInfo("C:\\Users\\Shen\\Desktop\\LenzOCR\\TempFolder\\tempPic.jpg"); 
} 


     private void ScanBT_Click(object sender, EventArgs e) 
{ 
    var folder = @"C:\Users\Shen\Desktop\LenzOCR\LenzOCR\WindowsFormsApplication1\ImageFile"; 

    DirectoryInfo directoryInfo; 
    FileInfo[] files; 
    directoryInfo = new DirectoryInfo(folder); 
    files = directoryInfo.GetFiles("*.jpg", SearchOption.AllDirectories); 

    var processImagesDelegate = new ProcessImagesDelegate(ProcessImages2); 
    processImagesDelegate.BeginInvoke(files, null, null);  

    //BackgroundWorker bw = new BackgroundWorker(); 
    //bw.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork); 
    //bw.RunWorkerAsync(bw); 
    //bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker1_RunWorkerCompleted); 
} 

private void ProcessImages2(FileInfo[] files) 
{ 
    var comparableImages = new List<ComparableImage>(); 

    var index = 0x0; 

    foreach (var file in files) 
    { 
     if (exit) 
     { 
      return; 
     } 

     var comparableImage = new ComparableImage(file); 
     comparableImages.Add(comparableImage); 
     index++; 
    } 

    index = 0; 

    similarityImagesSorted = new List<SimilarityImages>(); 
    var fileImage = new ComparableImage(singleFileInfo); 

    for (var i = 0; i < comparableImages.Count; i++) 
    { 
     if (exit) 
      return; 

     var destination = comparableImages[i]; 
     var similarity = fileImage.CalculateSimilarity(destination); 
     var sim = new SimilarityImages(fileImage, destination, similarity); 
     similarityImagesSorted.Add(sim); 
     index++; 
    } 

    similarityImagesSorted.Sort(); 
    similarityImagesSorted.Reverse(); 
    similarityImages = new BindingList<SimilarityImages>(similarityImagesSorted); 

    var buttons = 
     new List<Button> 
      { 
       ScanBT 
      }; 

    if (similarityImages[0].Similarity > 70) 
    { 
     con = new System.Data.SqlClient.SqlConnection(); 
     con.ConnectionString = "Data Source=SHEN-PC\\SQLEXPRESS;Initial Catalog=CharacterImage;Integrated Security=True"; 
     con.Open(); 

     String getFile = "SELECT ImageName, Character FROM CharacterImage WHERE ImageName='" + similarityImages[0].Destination.ToString() + "'"; 
     SqlCommand cmd2 = new SqlCommand(getFile, con); 
     SqlDataReader rd2 = cmd2.ExecuteReader(); 

     while (rd2.Read()) 
     { 
      for (int i = 0; i < 1; i++) 
      { 
       string getText = rd2["Character"].ToString(); 
       Action showText =() => ocrTB.AppendText(getText); 
       ocrTB.Invoke(showText); 
      } 
     } 
     con.Close(); 
    } 
    else 
    { 
     MessageBox.Show("No character found!", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error); 
    } 

} 
#endregion 
+0

如果註釋掉保存行,圖像框內是否顯示圖像?在調用Save之前,你確定該文件夾(包括TempFolder)存在嗎? – 2011-04-12 08:44:29

+0

@brathen:是的圖像顯示在圖片框和文件夾是肯定存在的 – joonshen 2011-04-12 10:53:19

回答

1

既然已經有一段時間,我希望你找到你的答案,但我要猜你需要設置,當你保存的JPEG文件格式:

area.Save(@"C:\Users\Shen\Desktop\LenzOCR\TempFolder\tempPic.jpg",System.Drawing.Imaging.ImageFormat.Jpeg); 

過去,我不記得picturebox控件是否是雙緩衝,這可能是問題(如果不是這樣,您可能無法在呈現時訪問它以用於保存目的,但是如果您請在設置picturebox3.runmage屬性之前設置一個區域的副本,以解決該問題):

Bitmap SavingObject=new Bitmap(area); 
picturebox3.Image=area; 
SavingObject.Save(@"C:\Users\Shen\Desktop\LenzOCR\TempFolder\tempPic.jpg",System.Drawing.Imaging.ImageFormat.Jpeg); 

無論如何,我希望你最終找到了你的解決方案(考慮到它已經發布了幾個月)。

+0

那麼,這解決了它對我:)感謝很多:) – 2013-01-27 13:19:59

0

這看起來像這個問題的一個副本: c# A generic error occurred in GDI+

相同的代碼,同樣的錯誤,同樣的作者。

看不出有什麼區別。但也許我錯過了一些東西。

相關問題