Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I need to save image but i got error.How to solved this error?
C#
byte[] pic = new byte[1];
                pic[0] = 0;
                if (pcbPhoto.Image != null)
                {
                    stream = new MemoryStream();
                    pcbPhoto.Image.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);// error occur here...
                    pic = stream.ToArray();
                }

error : A generic error occurred in GDI+.

Regards,
Karthikeyan,
Bangalore.
Posted
Updated 26-Sep-12 21:50pm
v3

Normally, this error occurs because the output stream is in use when you try to save, but since you are outputting to a MemoryStream that is not the case.

Sorry, but the problem is not in teh code you show - it's a lot further back...

The problem is probably that the stream you used to create the image in the first place is no longer available - it has been closed or disposed by the time you get to the Save method. And that doesn't work!
Try this:
When you load the image in the first place, create a new image as a copy of the actual picture, and use that - you can dispose of the original version you read in. For example:
C#
Image myImage;
using (Image inputImage = Image.FromFile(@"D:\Temp\MyPic.jpg"))
    {
    myImage = new Bitmap(inputImage);
    }
Because the new image is not associated with a file or stream, it can be saved with no problem.
 
Share this answer
 
Hi,
Use this code to read picture as Bytes array

C#
MemoryStream stream=new MemoryStream();

            pictureBox1.Image.Save(stream,System.Drawing.Imaging.ImageFormat.Jpeg);
            
            byte[] pic=stream.ToArray();


then insert pic into the database
C#
     SqlConnection con = new SqlConnection
                            ("Data Source=localhost;Initial Catalog=Northwind;Persist Security Info=True;User ID=sa;Password=123456");

         SqlCommand com = new SqlCommand("insert into Employees(LastName,FirstName,Photo) values('aaaa','bbb',@Photo)", con);

com.Parameters.AddWithValue("@Photo", pic);



Hope this helps you,
A.Mandour
 
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