Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hai ,

c# a generic error occurred in gdi+ generated when a Print Page.jpg
pls help me
Posted
Comments
Al Moje 26-Dec-11 2:41am    
Not clear question. Could you post your code so that we could see?...
vidya jobin 26-Dec-11 2:54am    
I'm trying to print an Image using PrintDocumentin C# but somehow the setting (like Number of Pages and Image Quality ) are ignored while printing and preview.

Is there anything wrong in following code, Am I missing something?

my code below
string mname="";
DialogResult result1 = MessageBox.Show("Are you want to Print the Bill", "", MessageBoxButtons.YesNo);
if (result1 == DialogResult.Yes)
{
Graphics g1 = this.CreateGraphics();
Image MyImage = new Bitmap(this.ClientRectangle.Width, this.ClientRectangle.Height, g1);
Graphics g2 = Graphics.FromImage(MyImage);
IntPtr dc1 = g1.GetHdc();
IntPtr dc2 = g2.GetHdc();
BitBlt(dc2, 0, 0, this.ClientRectangle.Width, this.ClientRectangle.Height, dc1, 0, 0, 13369376);
g1.ReleaseHdc(dc1);
g2.ReleaseHdc(dc2);
MyImage.Save(@"c:\PrintPage.jpg", ImageFormat.Jpeg);

FileStream fileStream = new FileStream(@"c:\PrintPage.jpg", FileMode.Open , FileAccess.ReadWrite );
StartPrint(fileStream, "Image");
fileStream.Close();

if (System.IO.File.Exists(@"c:\PrintPage.jpg"))
{
System.IO.File.Delete(@"c:\PrintPage.jpg");
}
if (type == "IPD")
{
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if (row.Cells["medname"].Value != null)
{
mname = mname + row.Cells["medname"].Value.ToString() + ",";
SqlCommand cmd = new SqlCommand("insert into Tmedicine values('" + Tid().ToString() + "','" + label7.Text + "','" + label6.Text + "','" + row.Cells["medname"].Value + "','" + row.Cells["mdate"].Value + "','" + row.Cells["intake"].Value + "','" + row.Cells["unit"].Value + "','" + label4.Text + "','" + row.Cells["unitprice"].Value + "','" + row.Cells["qty"].Value + "','" + row.Cells["total"].Value + "')", con);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}
else
{
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if (row.Cells["medname"].Value != null)
{
mname = mname + row.Cells["medname"].Value.ToString() + ",";
SqlCommand cmd = new SqlCommand("insert into OutMedical values('" + oTid().ToString() + "','" + label7.Text + "','" + label6.Text + "','" + row.Cells["medname"].Value + "','" + row.Cells["mdate"].Value + "','" + row.Cells["intake"].Value + "','" + row.Cells["unit"].Value + "','" + label4.Text + "','" + row.Cells["unitprice"].Value + "','" + row.Cells["qty"].Value + "','" + row.Cells["total"].Value + "')", con);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}

SqlCommand com = new SqlCommand("insert into phpayment values('" + label3.Text + "','" + label4.Text + "','"+DateTime.Now.ToShortTimeString()+"','" + label7.Text + "','" + label6.Text + "','" + label10.Text + "','"+mname.ToString()+"','" + textBox1.Text + "')", con);
con.Open();
com.ExecuteNonQuery();
con.Close();
billid();
}

Check the permission to the folder where you are saving this JPEG file.

folder should be have write permissions
 
Share this answer
 
I faced the same issue. In my scenario all are working fine,but getting exception when saving the image.the problem is i am saving the file with special character (\), which is not acceptable. now it is working fine after changed the file name.
 
Share this answer
 
add write permission
ASP User , IUSER , NETWORK SERVICES
 
Share this answer
 
Comments
Thomas Daniels 13-Jan-13 10:15am    
Why do you answer to a question from 2011? The question is solved already.
Rohini Shirke 22-Apr-13 6:18am    
MemoryStream s = new MemoryStream();
logoimage.Image.Save(s, System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] pic = s.ToArray();



I have write this code.this gives error "a generic error occurred in gdi+"
MemoryStream s = new MemoryStream();
logoimage.Image.Save(s, System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] pic = s.ToArray();



I have write this code.this gives error "a generic error occurred in gdi+"
 
Share this answer
 
Comments
[no name] 22-Apr-13 7:56am    
So? How exactly is this an answer to this 2 year old question that is already solved?
public static byte[] ConvertImageToByteArray(Image imageToConvert)
{
using (var ms = new MemoryStream())
{
Bitmap bmp = new Bitmap(imageToConvert);
bmp.Save(ms, format);
return ms.ToArray();
}
}
 
Share this answer
 
Comments
Momoaal 25-May-16 6:05am    
Thanks Moh'd A.Hafed1K , ;)
This solution works for me .. after long time trying to solve it ...
Great work ...
Juniper 22-May-20 9:25am    
This solution worked. Thanks so much Moh'd A.Hafed!
I had this problem and read this solutions.
but when i checked my project's folder which images saved there , i understood that there is not the expected folder in my project.
so i created a new folder again for my images and my problem have solved.
 
Share this answer
 

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