[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How to convert binary data to image

Posted on 2009-02-11
29
Medium Priority
?
2,913 Views
Last Modified: 2013-11-07
I am getting in a request Binary Format of a image. i need to convert it to originalimage.png and thumb nail.png and save it as attachment in sharepoint list. Can any one help me out?
0
Comment
Question by:bhanuug
  • 15
  • 14
29 Comments
 
LVL 15

Expert Comment

by:aibusinesssolutions
ID: 23613500
How are the files being posted?  If you are getting the files from an asp.net form post using a FileUpload control, you can use FileUpload1.SaveAs() to save the files to disk.

If not you could use something like this in your Page_Load function.
Dim files as HttpFileCollection = Page.Request.Files
For i = 0 To Files.Count - 1
   Dim file As HttpPostedFile = files(i)
        files.SaveAs(Server.MapPath("~/files/ & file.FileName))
Next
0
 
LVL 15

Expert Comment

by:aibusinesssolutions
ID: 23613510
Sorry that should be file.SaveAs, so

Dim files as HttpFileCollection = Page.Request.Files
For i = 0 To files.Count - 1
   Dim file As HttpPostedFile = files(i)
   file.SaveAs(Server.MapPath("~/files/" & file.FileName))
Next
0
 

Author Comment

by:bhanuug
ID: 23616178
I think you understood the question wrongly.
I am getting a Query string value (e.g: lang=en&image=%C2%89PNG%0D%0A%1A%0A&action=create)
i need to take the Image parameter and convert it to image file.
0
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 
LVL 15

Expert Comment

by:aibusinesssolutions
ID: 23616208
Oh I see, you can use the Image class to open the file as a stream.

Dim img As Drawing.Image = Drawing.Image.FromFile(Server.MapPath("~/image-folder/" & Server.UrlDecode(Request.QueryString("image"))))

0
 

Author Comment

by:bhanuug
ID: 23616491
Thanks for the Quick response. i have to convert that binary image to thumbnail.png and originalsize.png files and save as attachments in sharepoint. I dont want to save in filesystem. is there a way to convert the image with out saving to filesystem?
0
 
LVL 15

Expert Comment

by:aibusinesssolutions
ID: 23616529
Is the image stored on the server?  You are just passing the image name as a variable, you have to open the image from somewhere. If the file was posted with an upload control you could use the PostedFile.InputStream.
0
 

Author Comment

by:bhanuug
ID: 23616660
its not the name of the file that is passed. it is the image in binary format.
0
 
LVL 15

Expert Comment

by:aibusinesssolutions
ID: 23616776
Oh I see.  You could use a MemoryStream, something like the code below, but what are you using to save it in to Sharepoint?  Doesn't sharepoint store everything in a SQL database?

Dim sw As New MemoryStream()
Dim buff As Byte() = Text.Encoding.ASCII.GetBytes(Request.QueryString("image"))
sw.Write(buff, 0, buff.Length)
Dim img As Drawing.Image = Drawing.Image.FromStream(sw)
sw.Close()
0
 

Author Comment

by:bhanuug
ID: 23617038
image='%c2%89PNG%0d%0a%1a%0a
        Dim sw As New MemoryStream
        Dim buff As Byte() = Text.Encoding.ASCII.GetBytes(Request.QueryString("image"))
        sw.Write(buff, 0, buff.Length)
        Dim img As Drawing.Image = Drawing.Image.FromStream(sw)
        sw.Close()

gives me error at
Invalid parameter used. 
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 
 
Exception Details: System.ArgumentException: Invalid parameter used.
 
Source Error: 
 
 
Line 29:         Dim buff As Byte() = Text.Encoding.ASCII.GetBytes(Request.QueryString("image"))
Line 30:         sw.Write(buff, 0, buff.Length)
Line 31:         Dim img As Drawing.Image = Drawing.Image.FromStream(sw)
Line 32:         sw.Close()
Line 33: 
 
 
Source File: C:\Inetpub\wwwroot\ImageEncode\WebForm1.aspx.vb    Line: 31 
 
Stack Trace: 
 
 
[ArgumentException: Invalid parameter used.]
   System.Drawing.Image.FromStream(Stream stream, Boolean useEmbeddedColorManagement, Boolean validateImageData)
   System.Drawing.Image.FromStream(Stream stream, Boolean useEmbeddedColorManagement)
   System.Drawing.Image.FromStream(Stream stream)
   ImageEncode.WebForm1.Page_Load(Object sender, EventArgs e) in C:\Inetpub\wwwroot\ImageEncode\WebForm1.aspx.vb:31
   System.Web.UI.Control.OnLoad(EventArgs e)
   System.Web.UI.Control.LoadRecursive()
   System.Web.UI.Page.ProcessRequestMain()
 
 

Open in new window

0
 

Author Comment

by:bhanuug
ID: 23617385
image=%c2%89PNG%0d%0a%1a%0a
        Dim sw As New MemoryStream
        Dim buff As Byte() = Text.Encoding.ASCII.GetBytes(Request.QueryString("image"))
        sw.Write(buff, 0, buff.Length)
        Dim img As Drawing.Image = Drawing.Image.FromStream(sw)
        sw.Close()

gives me error at

2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
 Invalid parameter used. 
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 
 
Exception Details: System.ArgumentException: Invalid parameter used.
 
Source Error: 
 
 
Line 29:         Dim buff As Byte() = Text.Encoding.ASCII.GetBytes(Request.QueryString("image"))
Line 30:         sw.Write(buff, 0, buff.Length)
Line 31:         Dim img As Drawing.Image = Drawing.Image.FromStream(sw)
Line 32:         sw.Close()
Line 33: 
 
 
Source File: C:\Inetpub\wwwroot\ImageEncode\WebForm1.aspx.vb    Line: 31 
 
Stack Trace: 
 
 
[ArgumentException: Invalid parameter used.]
   System.Drawing.Image.FromStream(Stream stream, Boolean useEmbeddedColorManagement, Boolean validateImageData)
   System.Drawing.Image.FromStream(Stream stream, Boolean useEmbeddedColorManagement)
   System.Drawing.Image.FromStream(Stream stream)
   ImageEncode.WebForm1.Page_Load(Object sender, EventArgs e) in C:\Inetpub\wwwroot\ImageEncode\WebForm1.aspx.vb:31
   System.Web.UI.Control.OnLoad(EventArgs e)
   System.Web.UI.Control.LoadRecursive()
   System.Web.UI.Page.ProcessRequestMain()
 
 

Open in new window

0
 
LVL 15

Expert Comment

by:aibusinesssolutions
ID: 23617510
I see, it looks like the PNG data is URL Encoded.  This works for me.

Dim image As String = Server.UrlDecode("%c2%89PNG%0d%0a%1a%0a")
Dim buff() As Byte = Text.Encoding.ASCII.GetBytes(image)
Response.BinaryWrite(buff)
Response.End()
0
 

Author Comment

by:bhanuug
ID: 23617566
Are you able to see the image?

its displaying following result not the image?
?PNG 
i tried removing PNG from the image then its giving me this on the screen
? 
0
 
LVL 15

Expert Comment

by:aibusinesssolutions
ID: 23617775
The value %c2%89PNG%0d%0a%1a%0a isn't a valid PNG file.  I see what you are trying to do, but the binary data from a PNG file is going to be a very long string.  Are the PNG files very very small?  IE can only support 2083 characters in the querystring, which would be about a 1k png file.
0
 

Author Comment

by:bhanuug
ID: 23617830
Yes its a small file but not sure of size. How do i convert an image to the inline image(%c2%89PNG%0d%0a%1a%0a) so that i can test it locally ?
0
 
LVL 15

Expert Comment

by:aibusinesssolutions
ID: 23617873
I don't think it is an image.  I just created a 5x5 white png, and converted it to UTF8, then URL Encoded it, and this is what the bytes look like.

%ef%bf%bdPNG%0d%0a%1a%0a%00%00%00%0dIHDR%00%00%00%06%00%00%00%06%08%06%00%00%00%ef%bf%bd%ef%bf%bd%ef%bf%bdH%00%00%00%04gAMA%00%00%ef%bf%bd%ef%bf%bd%0b%ef%bf%bda%05%00%00%00%09pHYs%00%00%0e%ef%bf%bd%00%00%0e%ef%bf%bd%01%15(J%ef%bf%bd%00%00%00%11IDAT%18Wc%60%60%60%ef%bf%bd%ef%bf%bd%03%0f%ef%bf%bd%04%00L%ef%bf%bd%23%ef%bf%bd%25%3d%ef%bf%bdY%00%00%00%00IEND%ef%bf%bdB%60%ef%bf%bd
0
 

Author Comment

by:bhanuug
ID: 23617914
can you send me code to convert a png to percent encoding? so that i can test it on my machine.
0
 

Author Comment

by:bhanuug
ID: 23617952
i tried  using
%ef%bf%bdPNG%0d%0a%1a%0a%00%00%00%0dIHDR%00%00%00%06%00%00%00%06%08%06%00%00%00%ef%bf%bd%ef%bf%bd%ef%bf%bdH%00%00%00%04gAMA%00%00%ef%bf%bd%ef%bf%bd%0b%ef%bf%bda%05%00%00%00%09pHYs%00%00%0e%ef%bf%bd%00%00%0e%ef%bf%bd%01%15(J%ef%bf%bd%00%00%00%11IDAT%18Wc%60%60%60%ef%bf%bd%ef%bf%bd%03%0f%ef%bf%bd%04%00L%ef%bf%bd%23%ef%bf%bd%25%3d%ef%bf%bdY%00%00%00%00IEND%ef%bf%bdB%60%ef%bf%bd

with your code
Dim image As String = Server.UrlDecode("")
Dim buff() As Byte = Text.Encoding.ASCII.GetBytes(image)
Response.BinaryWrite(buff)
Response.End()

It still does not show me the image. its showing this
?PNG  IHDR???HgAMA?? ?a pHYs??(J?IDATWc```???L?#?%=?YIEND?B`?
0
 
LVL 15

Expert Comment

by:aibusinesssolutions
ID: 23618044
Yes, I did some testing and it looks like the PNG images were encoded with UTF7.

Here is the code to convert a PNG to a UTF7 URL encoded string.
Dim mem As New IO.MemoryStream()
Dim img As Drawing.Image = Drawing.Image.FromFile(Server.MapPath("~/Image1.png"))
img.Save(mem, Drawing.Imaging.ImageFormat.Png)
img.Dispose()
Response.Write(Server.UrlEncode(Text.Encoding.UTF7.GetString(mem.GetBuffer)))
mem.Close()
Response.End()

Open in new window

0
 
LVL 15

Expert Comment

by:aibusinesssolutions
ID: 23618051
Here is the same 5x5 image in UTF7, notice the first few characters are the same as what you have:

%c2%89PNG%0d%0a%1a%0a%00%00%00%0dIHDR%00%00%00%06%00%00%00%06%08%06%00%00%00%c3%a0%c3%8c%c3%afH%00%00%00%04gAMA%00%00%c2%b1%c2%8f%0b%c3%bca%05%00%00%00%09pHYs%00%00%0e%c3%82%00%00%0e%c3%82%01%15(J%c2%80%00%00%00%11IDAT%18Wc%60%60%60%c3%b8%c2%8f%03%0f%c2%9c%04%00L%c3%96%23%c3%9d%25%3d%c3%bdY%00%00%00%00IEND%c2%aeB%60%c2%82%00%00%00%00%00%00%00%00%00%00%00%00%00
0
 

Author Comment

by:bhanuug
ID: 23618161
So the image is passed correctly. Is it possible to save in the file system?
0
 
LVL 15

Expert Comment

by:aibusinesssolutions
ID: 23618307
Well, if the images were saved as UTF7, then there is a problem.  UTF7 only goes up to %C3, or 192, and will not convert your images correctly.  Can you paste the first few lines of binary data that you have?

A good way to test the data to make sure it is valid is to use the data: tag in an image, like so:
<img src="data:image/png,
%89PNG%0D%0A%1A%0A%00%00%00%0DIHDR%00%00%00%10%00%00%00%10%08%06%00%00%00%1F%F3%FFa%00%00%00%04gAMA%00%00%AF%C87%05%8A%E
9%00%00%00%19tEXtSoftware%00Adobe%20ImageReadyq%C9e%3C%00%00%02%29IDAT8%CBu%D3%3DhTA%14%86%E1%F7%CC%CC%DDl%B2nv%15%7F%82
%92B0I%25VA%8B%18Q%8B%14%9AJ%1BA0%A5%28Z%89%A0%9D%A0%B1%B0%97%60%21%04%AC%B4N%27%12%13Q%12%0C%A8e%88i%92%88%24%B8%C6%B8%
FFw%EE%1C%8B%BB%82%92%DD%D3L3%E7c%CE332v%EF%C3%CB%DE%7Ct%CC%18%8A%8A%26%20%88%2A%0D%AF%95%9Dj%F3y%B3Tz6%3F5%5E%A7C%C9%E5
G%8B%9F%5E%DD%1F%CEm%96%83%15%20%A8%12%27%01gaza%9B%A5%C5%8D7%DF%D77n%CF%3D%BDPm%17%60%8CHO%A9%AAveK%F9%FA%23%B0%BC%99%F
0y%3DfO%97cd%A8%C8%E8H%FF%B9%DC%FE%C2%DDN%270%0Ab%05%B2%0E2V%C8X%21%9B1ln%D7%18%DA%0B%13%27%0B%F4%F7%F5%5C%ED%14%E0%00D%
20%B2%A0%0Aj%85%AC3%CC.%FF%3E%DA%F0%1E%EF%03%C7%07%0Fqk%FAKE%82%CAN%DD%AF%AF%AD%D5_4K%A5%27%F3S%E35%A7%80%20XcZ%2AJ%08J%
23%8E%B9%7E%F60%5B%E5%40j%D3%D7%D3%B2%19%98%5E%D8%7E%B0%B4%B81%3Czc%E6%8AS%85%90v%22%04%AC%40%C6%19%92D%F9YUV%B6%14c%94%
D8%07vj%9E3%83%DD%8C%0C%15%C9e%CC%C5%99%D7%E5%C7NU%11%C0%190b%08%0AV%40I%D7%ACKg%14%FD%D7%26%E2%C4%C1%02%B3s%5D7%0DjH%82
o%7F%C7-%1B%27%E0Z6%AB%25%E1%DDj%83_5%25r%06%27jm%C3%97%81%E2%7F%CD%9Dl%E8%B2%88%11%AC%B5%28%E0%E2f%A3%12%99%7C%AE%90%8D
Q%05%23%82H%9A%D0%CEFT%09A%10%11T%C1%F9D%BF%5D%9B%FCx%A0%E9%29%80%D6%20%ED%CE%17%2C%13%A7%8F%EC%B21%01%C8%40%B7%03U%C5%C
DL%9E%1Ak7%FF%A5%87Kq%12%BCk%3D%95%5D%15%D4C%10L%C7O%A2%D6%A56%ED%AB%E1%CBH0%E2%3Am%A8%D6%EA%A5%C8%E4%F7%ED%B2%21%1D%D2%
9A%5E%82%FA%A6%A8j%DB%80%F3w%DE%BF%B5F%06%C0%F4%81V%FE%DA%B4X%25hR.%E4%A2%EE%3F%17%97%FF%1B%29%ED%A4z%00%00%00%00IEND%AE
B%60%82
" />

Open in new window

0
 

Author Comment

by:bhanuug
ID: 23618528
I tried adding the image tag on the aspx page But it is not showing the image. it is showing No Image "cross" on the page.
TEST.bmp
0
 
LVL 15

Expert Comment

by:aibusinesssolutions
ID: 23618567
The one I pasted?  Here's how it looks for me.
Capture.JPG
0
 
LVL 15

Expert Comment

by:aibusinesssolutions
ID: 23618660
Ok, I found a very simple way to convert from url encoded in to a byte()
I tested it and it worked on the string in my previous post.

Dim image As Byte() = HttpUtility.UrlDecodeToBytes(" IMAGEDATA ")

That will put the percent encoded data into a byte array, you can then do whatever you need to do with it, to test it you can do this.

Response.BinaryWrite(image)
Response.End()
0
 

Author Comment

by:bhanuug
ID: 23622344
I tried this already. Did you try putting the byte in memory stream and convert it to image it gives me same invalid parameter error. If it works for u please let me know.
0
 

Author Comment

by:bhanuug
ID: 23622661
Here is the code and png file
     Dim mem As New IO.MemoryStream
        Dim memImage As New IO.MemoryStream
        Dim img As Drawing.Image = Drawing.Image.FromFile("c:\test.png")
        img.Save(mem, Drawing.Imaging.ImageFormat.Png)
        img.Dispose()
        Dim strimage As String = Server.UrlEncode(Text.Encoding.UTF7.GetString(mem.GetBuffer))
        mem.Close()
        'Response.Write(strimage)
        'Response.End()
        Dim image As Byte() = HttpUtility.UrlDecodeToBytes(strimage)
        memImage.Write(image, 0, image.Length)
 
        Dim img1 As Drawing.Image = Drawing.Image.FromStream(memImage)
        memImage.Close()

Open in new window

test.png
0
 
LVL 15

Expert Comment

by:aibusinesssolutions
ID: 23627975
Ok, the initial code that I gave you that uses UTF7 will not work, since UTF7 only goes up to hex value C3, and as you know hex goes up to FF.

I played aorund with the HttpUtility class and came up with this, it works perfectly on your toast picture.

By the way, that toast picture turns out to be 15k worth of data, there's no way you could put that in a URL querystring.
Dim fs As New IO.FileStream(Server.MapPath("~/test.png"), IO.FileMode.Open, IO.FileAccess.Read)
Dim buff(fs.Length) As Byte
fs.Read(buff, 0, fs.Length)
fs.Close()
Dim encodedBuff() As Byte = HttpUtility.UrlEncodeToBytes(buff)
 
'After you get here, your binary data is in the encodedBuff, if you want to see the URL encoded data, you can do this:
Response.BinaryWrite(encodedBuff)
 
'To convert the data back to normal bytes, do this:
Dim img As Byte() = HttpUtility.UrlDecodeToBytes(encodedBuff)
Response.BinaryWrite(img)

Open in new window

0
 
LVL 15

Accepted Solution

by:
aibusinesssolutions earned 2000 total points
ID: 23628019
Also, I don't know if you noticed, the query string that you are getting has an improperly formatted PNG file header, the %c2 at the beginning should not be there, and it only occurs if the file was formatted in UTF7, which will not work.

A properly encoded PNG will start with %89PNG
0
 

Author Closing Comment

by:bhanuug
ID: 31545639
Thank you so much for your help. I asked client to send me the Base64 string instread of UTF-7 format. Now it works fine.
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question