1

I am trying to save a picture to database:

I am getting an error on this line:

Dim stream As New MemoryStream()

PictureBox1.Image.Save(stream, Imaging.ImageFormat.Png)

Is there a direct way to save a picture to memory stream without having to put it in picture box and then save it to the memory stream from the picture box?

ADyson
  • 57,178
  • 14
  • 51
  • 63
KGD
  • 19
  • 4
  • what error, exactly? – ADyson May 18 '20 at 19:44
  • "Is there a direct way"...well where is the picture data coming from originally? that isn't clear. – ADyson May 18 '20 at 19:45
  • There error is: System.NullReferenceException: 'Object reference not set to an instance of an object.' And for the picture I only specified its location for example: C:Users/..... – KGD May 18 '20 at 20:12
  • 1
    @KGD That probably means that `PictureBox1.Image` is null (A.K.A `Nothing`). If the PictureBox doesn't have an image, well, it's pretty obvious then, I guess. If, on the other hand, you _think_ the PictureBox has an image, you need to show us how you loaded the image into the PictureBox. That's because there are a few cases where you can see an image on the PictureBox but it's actually null (e.g., disposed or not loaded the first place). – 41686d6564 stands w. Palestine May 18 '20 at 20:23
  • @KGD are you sure that you're setting the Image's Image property and not the BackgroundImage property? – David May 18 '20 at 20:24
  • 2
    [Fie.ReadAllBytes](https://learn.microsoft.com/en-us/dotnet/api/system.io.file.readallbytes) – Jimi May 18 '20 at 20:27
  • I just load it by: PictureBox1.ImageLocation = "C:\Users\...\test.png" Is that the wrong way to do it? – KGD May 18 '20 at 20:55
  • If so let me know the correct way to load it – KGD May 18 '20 at 20:55
  • For example, does this answer your question? [how to load a file into memory stream](https://stackoverflow.com/questions/6213993/how-to-load-a-file-into-memory-stream) – ADyson May 18 '20 at 21:12
  • I manage to screen capture what I did. Can you advise me what is wrong in it. Is it the loading of the picture to the picture box or something else. http://prntscr.com/sj8z41 – KGD May 18 '20 at 21:25
  • @ADyson I dont think this is the thing I need – KGD May 18 '20 at 21:25
  • 1
    why not? It shows you how to load data directly from a file into a memorystream. Isn't that what you asked for? I was thinking of the last answer specifically: https://stackoverflow.com/a/6214032/5947043 – ADyson May 18 '20 at 21:29
  • I manage it to work. Thanks! – KGD May 18 '20 at 21:37

0 Answers0