Click here to Skip to main content
16,001,882 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want to display the image contained in a binary file in a picture box and more importantly save the binary file to a jpeg file.

[Copied from comments]
The binary file gets created from the following code:
VB
Private Shared Sub ExportJpegImage(image As PdfDictionary, ByRef count As Integer)
    Dim stream As Byte() = image.Stream.Value 
    Dim fs As New FileStream([String].Format("Image{0}.jpeg", System.Math.Max(System.Threading.Interlocked.Increment(count), count - 1)), FileMode.Create, FileAccess.Write)
    Dim bw As New BinaryWriter(fs)
    bw.Write(stream)
    bw.Close()
End Sub
Posted
Updated 6-Dec-15 1:39am
v2
Comments
Kornfeld Eliyahu Peter 6-Dec-15 3:10am    
JPEG is a binary image format. As BMP or PNG or GIf are...
So you have to explain what do you mean by binary here - a raw image? How it stored? What the origin of it?
Member 10628309 6-Dec-15 3:33am    
The binary file gets created from the following code:
Private Shared Sub ExportJpegImage(image As PdfDictionary, ByRef count As Integer)
Dim stream As Byte() = image.Stream.Value
Dim fs As New FileStream([String].Format("Image{0}.jpeg", System.Math.Max(System.Threading.Interlocked.Increment(count), count - 1)), FileMode.Create, FileAccess.Write)
Dim bw As New BinaryWriter(fs)
bw.Write(stream)
bw.Close()
End Sub
Kornfeld Eliyahu Peter 6-Dec-15 3:49am    
So you are loading a JPEG, store it as 'binary' (but actually done no change so it is still a JPEG) and now you are asking how to store it as JPEG again?
It seems to me that you do not understand what image is, but most you do not understand even a single line of the code you copied here...
Richard MacCutchan 6-Dec-15 8:30am    
A PdfDictionary is not an image, so you cannot do that.

1 solution

Assuming your "binary file" contains image data, that is, the data about the color (and, optionally, alpha channel value, opacity) for each pixel in the format you know, but not one of the standard image formats, you can solve the problem in the following steps:
  1. Create the instance of the class System.Drawing.Bitmap of required pixel size and pixel format:
    https://msdn.microsoft.com/en-us/library/7we6s1x3(v=vs.110).aspx[^],
    https://msdn.microsoft.com/en-us/library/3z132tat(v=vs.110).aspx[^].
  2. Get access to the bitmap pixel data. Using LockBits and two nested loops by x and y indices, copy your data from your file to the pixels of the image: https://msdn.microsoft.com/en-us/library/system.drawing.bitmap.lockbits%28v=vs.110%29.aspx[^].

    You can find the code sample on the MSDN help pages one of the two functions. See also: https://msdn.microsoft.com/library/ms229672%28v=vs.90%29[^].
  3. Save the instance of the bitmap obtained as JPEG file:
    https://msdn.microsoft.com/en-us/library/ms142147(v=vs.110).aspx[^],
    https://msdn.microsoft.com/en-us/library/system.drawing.imaging.imageformat(v=vs.110).aspx[^].


—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900