Jump to content

Howto save image to SQL database


Recommended Posts

Hello everybody!

I want to save images in Paint.Net to my SQL database in image-fields. The problem I get is that the byte stream is not a valid image when I get it back from the database.

The SaveForm and TransferPictures are classes I have implemented myself and they work fine. The problem can be the SaveToStream method I use in the Document class.

Does anyone have an idea?

The code is:

 
       private void menuFileSave_Click(object sender, System.EventArgs e)
       {
               SaveForm sf = new SaveForm();
               sf.ShowDialog();
               string fileName = sf.getFilename();

               Stream stream = null;

               stream = (Stream)new FileStream(fileName, FileMode.Create, FileAccess.ReadWrite);

               stream = workspace.Document.SaveToStream1(stream, null);
               stream.Position = 0;

               byte[] fileByte = new byte[stream.Length];

               int offset = 0;
               int remaining = fileByte.Length;

               stream.Read(fileByte, offset, remaining);

               TransferPictures trans = new TransferPictures();
               trans.uploadFileToDatabase(fileByte, fileName);

        }

Thanks!

Link to comment
Share on other sites

  • 4 months later...

This is what i did. I was saving it to a webservice as a byte[] and then saving the byte array to sql. I also flattened the image first, which was the same way the current save works. This was all done in a dialog box, as i wanted the code to be eay to upgrade later on. All i have to do it create a new save item and call the dialog passing the document to it.

I also have code to get a picture from SQL and place it into a new document. The way the paint.net guys have done the code makes it really easy to move the images in and out of sql.

        
private void SaveWSImage()
       {
           try
           {
               // flatten the document
               Document mydoc = privDoc;
               System.IO.MemoryStream myoutput = new System.IO.MemoryStream();

               using (Surface surface = new Surface(mydoc.Width, mydoc.Height))
               {
                   surface.Clear(ColorBgra.FromBgra(255, 255, 255, 0));
                   using (RenderArgs ra = new RenderArgs(surface))
                   {
                       mydoc.Render(ra, true);
                   }
                   using (Bitmap bitmap = surface.CreateAliasedBitmap())
                   {
                       //LoadProperties(bitmap, myinput);
                       bitmap.Save(myoutput, System.Drawing.Imaging.ImageFormat.Png);
                   }
               }
               WebService1.Images ws = new WebService1.Images();
               byte[] oFileByte = myoutput.ToArray();
               ws.SaveImage(oFileByte, System.Convert.ToString(oFileByte.Length));
               //
           }
           catch
           { }
       }

Link to comment
Share on other sites

This is not really related to Paint.NET. Please discuss this elsewhere.

By the way, having a "catch { }" statement like that is a really bad way to write code.

Locked

The Paint.NET Blog: https://blog.getpaint.net/

Donations are always appreciated! https://www.getpaint.net/donate.html

forumSig_bmwE60.jpg

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
 Share

×
×
  • Create New...