Click here to Skip to main content
15,895,799 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have a ppt file which i have converted from ppt to image and then save it in db. it save successfully. then i retrieve it from db but it gives and error:
parameter is not valid
Image imag = Image.FromStream(stream);

here is the code for saving in db:
C#
protected void Button1_Click(object sender, EventArgs e)
    {
        ApplicationClass pptApplication = new ApplicationClass();
        Presentation pptPresentation = pptApplication.Presentations.Open("saifullah.ppt", MsoTriState.msoFalse,
        MsoTriState.msoFalse, MsoTriState.msoFalse);

        var slides = new List<string>();
       
        for (var i = 1; i <= pptPresentation.Slides.Count; i++)
        {
            string target = i+".jpeg".ToString();
           
           pptPresentation.Slides[i].Export(target, "jpg", width, height);

            string pic = target;
           
           
           
       con.Open();
       cmd = new SqlCommand("insert into images values('"+@pic+"')",con);
       cmd.ExecuteNonQuery();
       con.Close();
            
        }

        pptPresentation.Close();
}</string>


and here is for retreiving from db.

C#
MemoryStream stream = new MemoryStream();
        try
        {
            con.Open();
            cmd = new SqlCommand("select pics from images", con);
            byte[] image = (byte[])cmd.ExecuteScalar();

            stream.Write(image, 0, image.Length);

            Image imag = Image.FromStream(stream);
            Response.ContentType = "image/JPEG";
            imag.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
        }
        finally
        {
            con.Close();
            stream.Close();
        }

please tell me what i need to correct
Posted
Updated 27-Oct-11 0:18am
v2

You try to open a image from a stream with a empty stream !
Try This :
C#
con.Open();
             cmd = new SqlCommand("select pics from images", con);
             byte[] image = (byte[])cmd.ExecuteScalar();
MemoryStream stream = new MemoryStream(image );
....



and use 'pre' tag to enclose your code when you post question at codeproject !
 
Share this answer
 
v2
Comments
saifullahiit 27-Oct-11 6:44am    
i have tried your given suggestion but it gave same error
The problem is that what you have saved in your database is not an image - at best it is the name of a file containing an image.
So when you retrieve it, you try to use the file name as actual image data and it (understandably) complains.

Either: continue to save the file name in your database, but use Image.FromFile when you retrieve it, or load the image data into the database instead. Myself, I would go with the later.
 
Share this answer
 
Comments
Yuri Vital 27-Oct-11 6:40am    
Exaclty, i have not seen that the file name used instead of the file content.

Moreover , the following line is incorect to
cmd = new SqlCommand("insert into images values('"+@pic+"')",con);

It is incorect because any SqlParameter was given to the command...
For me corrert command was like :
cmd = new SqlCommand("insert into images values(@pict)",con);
SlParameter p = new SqlPamrameter("@pict");
p.Value = FileBytes;
cmd.Parameters.Add(p);
...
OriginalGriff 27-Oct-11 6:53am    
I did notice that - but thought I'd leave it until he'd sorted out what he actually wanted in his DB. :laugh:
saifullahiit 27-Oct-11 7:43am    
no when i cheked my db table the data was inserted as
<binary data="">
saifullahiit 27-Oct-11 6:48am    
it saved the images with the name of slides. means if the slide name is 1,2,3,4 so the image name is also 1,2,3,4
OriginalGriff 27-Oct-11 6:52am    
Yes, but that is all you are trying to save - not the jpg image data.

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