0

Using c#, I created a blank picture box control and a graphics object. I instantiated the graphics object by pictureBoxControl.CreateGraphics(). I allow the user to then draw on the picture box with the mouse. I'm trying to save the graphics object to a varbinary column in the database, but I can't figure that out.

Can somebody please help?

Mike Turner
  • 471
  • 1
  • 7
  • 22
  • Do you just want to save a binary serialization of the graphics or actually save it in some image format? – juharr Feb 03 '16 at 15:55
  • I just want to save a binary serialization of the graphics image, I do not want to save to a file – Mike Turner Feb 03 '16 at 16:08
  • You don't have to save to a file to save the data in an image format. The real question is how the data will be used. As an image forma it could be used by a lot of consumers. As a binary serialization it has to be deserialized first. – juharr Feb 03 '16 at 16:08
  • thanks. can you help me? how do I save the graphics as varbinary? I'm trying with memoryStream but I need an image for that – Mike Turner Feb 03 '16 at 16:10
  • Check out [`BinaryFormatter.Serialize`](https://msdn.microsoft.com/en-us/library/c5sbs8z9(v=vs.110).aspx). You can use a [`MemoryStream`](https://msdn.microsoft.com/en-us/library/system.io.memorystream(v=vs.110).aspx) to save the data to and then use it's `ToArray` method to get the `byte[]` to pass to the DB. – juharr Feb 03 '16 at 16:15
  • You might want to check this out on why you cannot convert a graphics object to an image http://stackoverflow.com/questions/2891346/convert-graphics-to-image-in-c-sharp Even if you save a binary representation of the graphics object I'm not sure you can really do anything with it when you pull it back out of the DB. – juharr Feb 03 '16 at 16:19
  • Actually if you are using a `PictureBox` then just get the image from [`PictureBox.Image`](https://msdn.microsoft.com/en-us/library/system.windows.forms.picturebox.image(v=vs.110).aspx) – juharr Feb 03 '16 at 16:22
  • Thanks much! I can't use BinaryFormatter.Serialize bec Graphics is not serializable - so that throws an error. When I do PictureBox.Image, I get null bec I never set an image. How do I set the graphics to the image? – Mike Turner Feb 03 '16 at 16:35
  • Instead of getting the graphics of the picture box. Create an image, get the graphics from that, draw stuff, and then set it to the picture boxes image. – juharr Feb 03 '16 at 16:37
  • cool. good idea. I created an image file and that worked! but I don't want all my users to have a file on their systems. is there any way to create this image in memory only? – Mike Turner Feb 03 '16 at 16:46
  • Sure, just do `var image = new Bitmap(width, height);` – juharr Feb 03 '16 at 16:49
  • thank you!!!! cool!! great help!! – Mike Turner Feb 03 '16 at 16:52
  • Do have a look at [this!](http://stackoverflow.com/questions/29772266/convert-graphics-object-to-bitmap/29777131?s=1|7.6244#29777131) - It explains why your aim makes no sense and what does make sense.. – TaW Feb 03 '16 at 20:57

0 Answers0