I have a jpeg file that is being held as a list(of Byte)
Currently I have code that I can use to load and save the jpeg file as either a binary (.jpeg) or a csv of bytes (asadsda.csv).
I would like to be able to take the list(of Byte) and convert it directly to a Picturebox without saving it to disk and then loading it to the picturebox.
If you are curious, the reason I get the picture file as a list of bytes is because it gets transfered over serial via an industrial byte oriented protocol as just a bunch of bytes.
I am using VB.net, but C# example is fine too.
You could do this:
var ms = new MemoryStream(byteList.ToArray());
pictureBox.Image = Image.FromStream(ms);
The Image class has a FromStream method and you can create a MemoryStream from a byte array. So:
MemoryStream ms = new MemoryStream(byteList.ToArray());
Image image = Image.FromStream(ms);
What you need to do is take the bytes and read them into a stream. You can then use the stream to load the picture box image.
using( MemoryStream ms = new MemoryStream( byteList.ToArray() ) )
{
this.pictureBox1.Image = Image.FromStream( ms );
}
Related
Yes you can just use:
// A byte array that contains a .jpeg data.
System.IO.Stream BitmapStream = System.IO.MemoryStream(byteBuffer);
System.Drawing.Bitmap MyImage = System.Drawing.Bitmap.FromStream(BitmapStream);
MyImage.Save("C:\Folder\Folder\image.jpeg", System.Drawing.Imaging.ImageFormat.Jpeg);
But how could you do this without System.Drawing?
I would like to write my own code to create the image.
If you have the image in bytes you don't need to use drawing to make an image, just save it as a binary file.
System.IO.File.WriteAllBytes("C:\Folder\Folder\image.png", byteBuffer);
I work on a project where i need to process a image i receive from a socket and to display it.
I'm getting the image in jpeg format,and i cannot just use the Image.FromStream()); method for retrieving the image,because it contains more data and i want to process it while i'm reading the data-for efficiency reasons.(Basically what i want is to read the image from the stream manually).
Is there any source which explaines how these image are stored in the MemoryStream?
The MemoryStream is built on a byte[] buffer,i resuse the same buffer also and i do not create a new MemoryStream everytime the method called.
A sample of code:
private byte[] BlockToJpeg()
{
Bitmap block=new Bitmap("...");
MemoryStream ms=new MemoryStream();
block.Save(ms, ImageFormat.Jpeg);
return ms.GetBuffer();
}
So the call would look like this
byte[] buffer=BlockToJpeg();
sck.Send(buffer);//sending the buffer...not the full code because this is not our problem.
Now in the Reciver side,when i'll get that buffer:
Byte[] RecieveBuffer=sck.Recieve();//again,kind of pseudo code,because this is not the relevant part.
i have to processes it's pixels,so i'll prefer to read them from the byte[] array one by one manually...
Is there any structure for reading this(in our case-reading a jpeg image stored as byte array)?
For example- first 4 bytes are width,second are height...3rd are PixelFormat and the rest are the pixels values...or somthing...?
Thanks.
jpeg is type of compression method usually on images.
you can read more about it here:http://www.ams.org/samplings/feature-column/fcarc-image-compression
I have a winform C# desktop application.
I have a constant stream of jpegs coming in.
I am comparing the current image with the previous 1.
By using a 3rd party tool - Emgu - I can create a new image that contains just the differences.
I then convert that image to a memory stream and then to a byte array.
In the receiving application I take this byte array and load the image via a memory stream using these bytes.
The trouble is that the image degrades quite a lot.
If I save the image to the hard drive before converting it to a memory stream on the client side the quality of the image is good.
The problem lies when i load it as a memory stream.
I encode it as jpeg.
If I encode it as a PNG before sending to the server the quality is good again.
The trouble with encoding to PNG the size in the byte array shoots up.
What my intention was all along was to reduce the number of bytes I have to upload to improve response time.
Am I doing something wrong or can this not be done?
This is my code:
Bitmap imgContainingDifference
= GetDiffFromEmgu(CurrentJpegImage, PreviousJpegImage);
using (System.IO.MemoryStream msIn = new System.IO.MemoryStream())
{
holding.Save(msIn, System.Drawing.Imaging.ImageFormat.Jpeg);
data = msIn.ToArray();
}
//test here
using (System.IO.MemoryStream msOut = new System.IO.MemoryStream(_data))
{
Bitmap testIMG = (Bitmap)Image.FromStream(msOut);
}
//result is image is poor/degrades
If I do this instead:
using (System.IO.MemoryStream msIn = new System.IO.MemoryStream())
{
holding.Save(msIn, System.Drawing.Imaging.ImageFormat.Png);
data = msIn.ToArray();
}
using (System.IO.MemoryStream msOut = new System.IO.MemoryStream(_data))
{
Bitmap testIMG = (Bitmap)Image.FromStream(msOut);
}
//Image is good BUT the size of the byte array is
//10 times the size of the CurrentFrame right at the start.
This is what the image looks like when using the kid suggestion from :
I have now tried using a encoder from the kind suggestion from #MagnatLU and I also get the same quality of image if I use FreeImage.Net.
You can set JPEG compression level when encoding your file to value that is the best empirical tradeoff between quality and size.
Actually the bitmap created has a file size larger than the input bitmap. I opened the bitmap that was created and it looked completely different to what i inputted. why is that ?
I read a bitmap through FileStream and then i write its contents to a bitmap object.Next i write it as a bitmap file onto harddisk. I cant figure out why the output bitmap is larger than the input bitmap. Could someone please help me.
Bitmap.Save (Image.Save) will, by default, save the image as in PNG format.
If you call Save with an ImageFormat value, you should get your bitmap.
b.Save("test.bmp", ImageFormat.Bmp);
When a bitmap file is created there is often padding added to each row to ensure that each row is a multiple of 4 bytes. When you read the bitmap file into a FileStream the padding is also read.
This can mean that the FileStream is larger than expected and when you write it to a bitmap it will display unexpected behaviour since when you write it to a new Bitmap it treats the padding as if it were your image data.
Sorry I did not understand your problem well. But did you try like this?
private System.Drawing.Bitmap readfromFile(string filename)
{
FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read);
byte[] bytes = new byte[fs.Length];
fs.Read(bytes, 0, Convert.ToInt32(fs.Length));
MemoryStream ms = new MemoryStream(bytes);
System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(ms);
return bmp;
}
and saved like this:
System.Drawing.Bitmap bmp = readfromFile("xxxx --- Path");
bmp.Save("test.bmp", ImageFormat.Bmp);
I tried that and it always returns the same image for me.
I'm trying to load an emf file in to an Image Object, however the RawFormat is always incorrect {[ImageFormat: b96b3cac-0728-11d3-9d7b-0000f81ef32e]} instead of Emf. I've tried loading from a file stream and memory stream. I've also tried creating a MetaFile object instead of an Image but to no avail. Is this a bug or there some other trick I don't know about?
MemoryStream stream = new MemoryStream(imageData);//imageData is a byte array
Image tempImage = Image.FromStream(stream);
Does this help?
System.Drawing.Imaging.ImageFormat.Emf.Guid = {b96b3cac-0728-11d3-9d7b-0000f81ef32e}
Dan