-1

I'm using vb.net framework 4.5 . From a 3th party I receive a CSV file. In one block their is a BMP image. I read it from the file as a string.

I want to convert this BMP to a JPG. I learned about the bitmap class in the .net framework. This could represent a bitmap and save it in the JPG format. However the bitmap class constructor takes a filename a parameter. That's not what I want. I tried to convert the string to an IO memory stream and read it into the bitmap class but this isn't working.

Can someone give me some pointer on methodes / classes to use to achieve this ?

Walt501
  • 53
  • 1
  • 3
  • 6

5 Answers5

1

Hard to give code when we don't know the format of the string...

...it might be a base 64 encoded string. Here's an example that shows the image in PictureBox1 being converted to a base 64 string, then back to an image and placed into PictureBox2:

Image via Base 64 String

    Dim oldMS As New System.IO.MemoryStream
    PictureBox1.Image.Save(oldMS, System.Drawing.Imaging.ImageFormat.Jpeg)
    Dim strData As String = Convert.ToBase64String(oldMS.ToArray)

    Debug.Print(strData)

    Dim bytes() As Byte = Convert.FromBase64String(strData) ' strData would come from your CSV file
    Dim MS As New System.IO.MemoryStream(bytes)
    Dim bmp As Image = Image.FromStream(MS)
    PictureBox2.Image = bmp

Here's what the string looks like:

/9j/4AAQSkZJRgABAQEAYABgAAD/4QBORXhpZgAATU0AKgAAAAgABAMBAAUAAAABAAAAPlEQAAEAAAABAQAAAFERAAQAAAABAAAAAFESAAQAAAABAAAAAAAAAAAAAYagAACxj//bAEMACAYGBwYFCAcHBwkJCAoMFA0MCwsMGRITDxQdGh8eHRocHCAkLicgIiwjHBwoNyksMDE0NDQfJzk9ODI8LjM0Mv/bAEMBCQkJDAsMGA0NGDIhHCEyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMv/AABEIACAAIAMBIgACEQEDEQH/xAAfAAABBQEBAQEBAQAAAAAAAAAAAQIDBAUGBwgJCgv/xAC1EAACAQMDAgQDBQUEBAAAAX0BAgMABBEFEiExQQYTUWEHInEUMoGRoQgjQrHBFVLR8CQzYnKCCQoWFxgZGiUmJygpKjQ1Njc4OTpDREVGR0hJSlNUVVZXWFlaY2RlZmdoaWpzdHV2d3h5eoOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3uLm6wsPExcbHyMnK0tPU1dbX2Nna4eLj5OXm5+jp6vHy8/T19vf4+fr/xAAfAQADAQEBAQEBAQEBAAAAAAAAAQIDBAUGBwgJCgv/xAC1EQACAQIEBAMEBwUEBAABAncAAQIDEQQFITEGEkFRB2FxEyIygQgUQpGhscEJIzNS8BVictEKFiQ04SXxFxgZGiYnKCkqNTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqCg4SFhoeIiYqSk5SVlpeYmZqio6Slpqeoqaqys7S1tre4ubrCw8TFxsfIycrS09TV1tfY2dri4+Tl5ufo6ery8/T19vf4+fr/2gAMAwEAAhEDEQA/AOG8R69rvjTWdTvpbyVYoGYw24chVUdFUDjO0E++PeuS+13P/PxL/wB9muo8OXzrqF5YgxEmVp4lk4BZc5GexI5/4DjvWRrGkm3uZ5raJltwxJjPLQg9AfUe9ekopLQy5veszO+13P8Az8S/99mur8Pa9rngzW9OvIL6RklZfPt95KkHBKMDxnawPtn2rE0vTd08E91EzwlspAv358dh6D1J/Wr+sXcv261s5PKD+cLiVYjkBmxgbu5xzn/ax2FTJJqw09TFa4ktdVe4iOJI5iynHvXeeGYrDUDDNfTTJJGjTxWSRbmkReSqyZ4DYIww/wDr874v8I6t4a8QXVpdWkxjMjGGYISsik5BBHtWZaz6haxSQrFP5Mi7XUKQcexxx0FHM+X3WEopvU6XxAtlZ3V39keZyQJp7V02sFbBAaTPIGQMKBXJRzSXOqJNKcu8wZjj3qe7lv7pEiMM4hjAVEKk8D1Pc8n+mK0/CPhLVvEmv2tpaWk2zzFMszIQsag5JJ+naknaPvMdknof/9k=
Idle_Mind
  • 38,363
  • 3
  • 29
  • 40
0

Bitmap also has a constructor accepting a Stream. You can read the data into a MemoryStream and pass that to the constructor.

How to implement this depends on your code, which you didn't provide.

Community
  • 1
  • 1
CodeCaster
  • 147,647
  • 23
  • 218
  • 272
0

Please refer to this post. Assuming your string is a valid string of bytes representing the image, this post should give you what you're looking for. The code examples are in C#, but they are simple enough to convert to VB.Net.

Community
  • 1
  • 1
Russ Wilson
  • 110
  • 10
0

Thanks for all the inspiration. This is what I did and what worked.

Dim bytes() As Byte
Dim MS As System.IO.MemoryStream
Dim ImageInStringFormat as string 
Dim NewImage As System.Drawing.Image

bytes = Convert.FromBase64String(ImageInStringFormat)
MS = New System.IO.MemoryStream(bytes)
NewImage = System.Drawing.Image.FromStream(MS)

NewImage.Save("c:\temp\try.jpg", System.Drawing.Imaging.ImageFormat.Jpeg)

Last remark, It turned out that the string I was using wasn't a valid Base64 string. You can check this by getting the length of the string and check if it can be divided by 4. Of not, at = to the end of the string until it is dividable by 4. This will bake it a valid Base64 string.

Walt501
  • 53
  • 1
  • 3
  • 6
0

A solution without having to save it to the filesystem:
I combined this question's answers + How to Convert System.Drawing.Image to Byte Array? to this:

Function ImageToString(ByVal Img As Image)
    Dim ImgConverter As New ImageConverter()
    Dim ImgBytes As Byte() = ImgConverter.ConvertTo(Img, GetType(Byte()))
    Return Convert.ToBase64String(ImgBytes)
End Function

I hope it helps someone :)

Community
  • 1
  • 1
Cr4xy
  • 179
  • 1
  • 8