Click here to Skip to main content
15,917,456 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all :)

I have this code, that create a thumbnial from another image, but when I save this image it gives me this error.

"A generic error occurred in GDI+."

Here is my code, all images is .Jpg.

C#
#region create thumnail
                string src = Server.MapPath("~/" + RadComboBox1.SelectedValue.ToString() + "/images/" + validFile.FileName);
                System.Drawing.Image image = System.Drawing.Image.FromFile(src);
                int srcWidth = image.Width;
                int srcHeight = image.Height;
                Bitmap bmp = new Bitmap(110, 74);

                System.Drawing.Graphics gr = System.Drawing.Graphics.FromImage(bmp);
                gr.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                gr.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
                gr.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;

                System.Drawing.Rectangle rectDestination = new System.Drawing.Rectangle(0, 0, 110, 74);
                gr.DrawImage(image, rectDestination, 0, 0, srcWidth, srcHeight, GraphicsUnit.Pixel);

                bmp.Save(src);

                bmp.Dispose();
                image.Dispose();
                #endregion


Any help please ? :)
Posted
Updated 1-Nov-11 4:32am
v3
Comments
89kksahu 26-Oct-12 1:26am    
==========================================
*******PLEASE HELP ON THIS****************
==========================================

I GOT A ERROR MESSAGE:
A GENERIC ERROR OCCURRED IN GDI+
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using Twaingui;

using GdiPlusLib;

namespace Twaingui
{
public partial class Form1 : Form, IMessageFilter
{
public Twain tw;
public bool msgfilter;
private int picnumber = 0;
public Form1()
{

InitializeComponent();
tw = new Twain();
tw.Init(this.Handle);
}
bool IMessageFilter.PreFilterMessage(ref Message m)
{
TwainCommand cmd = tw.PassMessage(ref m);
if (cmd == TwainCommand.Not)
return false;

switch (cmd)
{
case TwainCommand.CloseRequest:
{
EndingScan();
tw.CloseSrc();
break;
}
case TwainCommand.CloseOk:
{
EndingScan();
tw.CloseSrc();
break;
}
case TwainCommand.DeviceEvent:
{
break;
}
case TwainCommand.TransferReady: //ERROR IN THIS BOCK OF CODE
{
ArrayList pics = tw.TransferPictures();
EndingScan();
tw.CloseSrc();
picnumber++;
for (int i = 0; i < pics.Count; i++)
{
IntPtr img = (IntPtr)pics[i];


/*PicForm newpic = new PicForm(img);
newpic.MdiParent = this;
int picnum = i + 1;
newpic.Text = "ScanPass" + picnumber.ToString() + "_Pic" + picnum.ToString();
newpic.Show();
* */
System.Drawing.Bitmap bitmap = System.Drawing.Bitmap.FromHbitmap(img);
//pictureBox1.Image = Image.FromHbitmap(img);

}
break;
}
}

return true;
}

private void EndingScan()
{
if (msgfilter)
{
Application.RemoveMessageFilter(this);
msgfilter = false;
this.Enabled = true;
this.Activate();
}
}
private void SelectScaner_Click(object sender, EventArgs e)
{
tw.Select();
}

private void scan_Click(object sender, EventArgs e)
{
if (!msgfilter)
{
this.Enabled = false;
msgfilter = true;
Application.AddMessageFilter(this);

}
tw.Acquire();
}

private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
tw.Finish();
}

private void Form1_Load(object sender, EventArgs e)
{

}

private void panel1_Paint(object sender, PaintEventArgs e)
{

}

private void Exit_Click(object sender, EventArgs e)
{
Close();
}
}
}

The problem is that you are trying to save the new image over the top of the existing file: Because you loading the original image from the file, it is in use when you try to overwrite it. If you move the
C#
image.Dispose();
line uup to above the
C#
bmp.Save(src);
line, it will work.

BTW: you should also Dispose the Graphics object you created - they are a scarce resource!
 
Share this answer
 
It happens because you're trying to overwrite your original image at runtime. You have to save the thumbnail to a different image.

Try:

string src = Server.MapPath("~/" + RadComboBox1.SelectedValue.ToString() + "/images/" + validFile.FileName);
string thumbPath = Server.MapPath("~/" + RadComboBox1.SelectedValue.ToString() + "/images/" + "thumb" + validFile.FileName);


System.Drawing.Image image = System.Drawing.Image.FromFile(src);
int srcWidth = image.Width;
int srcHeight = image.Height;
Bitmap bmp = new Bitmap(110, 74);
 
System.Drawing.Graphics gr = System.Drawing.Graphics.FromImage(bmp);
gr.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
gr.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
gr.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
 
System.Drawing.Rectangle rectDestination = new System.Drawing.Rectangle(0, 0, 110, 74);
gr.DrawImage(image, rectDestination, 0, 0, srcWidth, srcHeight, GraphicsUnit.Pixel);
 

bmp.Save(thumbPath);

gr.Dispose(); 
bmp.Dispose();
image.Dispose();


P.S. Handy Piece of code overall :)
 
Share this answer
 
v5
Here I would make an indecent use of the using keyword. This way :

#region create thumnail
string src = Server.MapPath("~/" + RadComboBox1.SelectedValue.ToString() + "/images/" + validFile.FileName);

using (System.Drawing.Image image = System.Drawing.Image.FromFile(src))
using (Bitmap bmp = new Bitmap(110, 74))
using (System.Drawing.Graphics gr = System.Drawing.Graphics.FromImage(bmp))
{
   int srcWidth = image.Width;
   int srcHeight = image.Height;
   gr.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
   gr.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
   gr.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;

   System.Drawing.Rectangle rectDestination = new System.Drawing.Rectangle(0, 0, 110, 74);
   gr.DrawImage(image, rectDestination, 0, 0, srcWidth, srcHeight, GraphicsUnit.Pixel);
   string dest = <destination file name from your scheme>;
   bmp.Save(dest);
}
#endregion


This way everything that has to be is cleaned up at the end of the process, without having to close/dispose anything ; using will handle that for you.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900