0

Is there any way to draw on System.Drawings.Printing.PrintDocument item in My.Settings, lets name it as LastDocument.

I found that we can set it as same document of other PrintDocuments on Application forms which doesn't help me in this case. What I want is, the item, LastDocument, should save drawings from my selected PrintDocument from my Application form and retrieve it later.

So is there any method to do this.

I found one partial solution is to save the PrintDocument drawings as an image to My.Settings using the method mentioned by John here.

  • What you're suggesting doesn't actually make sense. A `PrintDocument` does contain the drawing. You will need to do as you already suggested and use the same `Graphics` methods to draw on an `Image` and you can then simply call `DrawImage` to print now and later, or else you will need to save all the data you need to be able to reproduce all the same calls to `Graphics` methods to produce the same drawing again later. – jmcilhinney Oct 30 '17 at 01:25
  • Is there a way I can **save** a bitmap or image to My.Settings? – Mohammed Julfikar Ali Mahbub Oct 30 '17 at 01:30
  • The way that would usually be done is to save the `Image` to a `MemoryStream`, get a `Byte` array from that and then call `Convert.ToBase64String` to get a `String` that you save to `My.Settings`. That's how Microsoft store images in XML. You can then do the inverse (`Convert.FromBase64String`, write to a `MemoryStream`, `Image.FromStream`) to get an `Image` object back again. Other than the base-64 bit, [click here](http://www.vbforums.com/showthread.php?469562-Saving-Images-in-Databases) to learn how. – jmcilhinney Oct 30 '17 at 01:36
  • Note that a `Bitmap` is an `Image`, as the `Bitmap` class inherits the `Image` class. If you call `Image.FromStream` or the like and the data represents a bitmap image, e.g. a JPEG, then the object created will be a `Bitmap`. `Image` is abstract (`MustInherit`) so you cannot have an `Image` object that isn't also some more specific type. – jmcilhinney Oct 30 '17 at 01:55
  • Hey @jmcilhinney This might be a dumb question to you but can you please tell me how can I save Byte collection in My.Setting as I can only see Byte but not ByteCollection or something like that.... – Mohammed Julfikar Ali Mahbub Oct 30 '17 at 02:16
  • I mean how can I add System.Byte() instead of System.Byte which is available normally. – Mohammed Julfikar Ali Mahbub Oct 30 '17 at 02:17
  • You don't save `Byte` arrays to `My.Settings`. You convert them to base-64 text and save that, as I have already described. – jmcilhinney Oct 30 '17 at 02:18
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/157767/discussion-between-mohammad-zulfikar-and-jmcilhinney). – Mohammed Julfikar Ali Mahbub Oct 30 '17 at 02:19
  • Nope. You've already got what you need. It's time to use it. – jmcilhinney Oct 30 '17 at 02:40
  • Okay, but can you say did you meant string collection while saving as string or just a single line string? – Mohammed Julfikar Ali Mahbub Oct 30 '17 at 02:57
  • NM I got it thanks a lot for your help mate! It means a lot to me! :) – Mohammed Julfikar Ali Mahbub Oct 30 '17 at 03:21

1 Answers1

0

Thank you jmcilhinney for your help. Now as I solved the problem per your guidelines and as I have solved it for me I took decision to write the answer what I did to solve the issue so the future viewers find it easy to solve.

  1. Double click on 'My Project' from 'Solution Explorer' and go to 'Settings', add new item with some name, here I will use LastDocument, and 'Type' as 'String'. Save all and close the tab.

  2. Back to your form from where you want to save the image to My.Settings.LastDocument and add this line to save the image to My.Settings.LastDocument,

    Dim mstream As New System.IO.MemoryStream pic.Image.Save(mstream, Imaging.ImageFormat.Png) Dim arrimage() As Byte = mstream.GetBuffer My.Settings.LastDocument = Convert.ToBase64String(arrimage)

  3. Now to get the picture back from settings use this,

    Dim arrimage() As Byte = Convert.FromBase64String(My.Settings.LastDocument) Dim mstream As New System.IO.MemoryStream(arrimage) Dim GetLastImg As Bitmap = New Bitmap(System.Drawing.Image.FromStream(mstream)) pic.Image = GetLastImg