Convert GD-Sharp stream to Bitmap - c#

im currently trying out GD-Sharp and wanted to convert the graphic into bitmap without saving it to image file.
GD-Sharp method to save to stream is
bool GB.Save(Stream outStream);
for saving using stream is
using(FileStream fs = File.OpenWrite(#"stream1.jpg"))
{
image.Save((System.IO.Stream)fs);
fs.Close();
}
since bitmap supports stream, how do it convert GD-Sharp to Bitmap ? thanks.

You can use a MemoryStream, something like
gdsBitmap.Save(memStream);
memStream.Seek(0);
gdiBitmap = Bitmap.FromStream(memStream);

i did something like this.
MemoryStream memStream = new MemoryStream();
gdimg.Save(memStream);
Bitmap bmp2 = new Bitmap(memStream);

Related

Serializing/Deserializing Bitmap causes the image to change

I have code that serializes/deserializes bitmap objects. For some image files (especially png files) I found out that the size of the file is reduced after deserializing it, and the image quality is reduced. And if I serialize the deserialized image again, the resulting byte array is different compared to what it was before. Has anyone experienced the same problem? If so, how were you able to solve it?
Here is my code snippet:
private byte[] GetBytes(Bitmap image)
{
BinaryFormatter formatter = new BinaryFormatter();
using (MemoryStream ms = new MemoryStream())
{
formatter.Serialize(ms, image);
byte[] _bytes = ms.ToArray();
return _bytes;
}
}
private Bitmap GetImage(byte[] _bytes)
{
BinaryFormatter formatter = new BinaryFormatter();
using (MemoryStream ms = new MemoryStream(_bytes))
{
Bitmap bmp = (Bitmap)formatter.Deserialize(ms);
return bmp;
}
}
PS: Using an Image object instead of Bitmap seems to solve the problem, but I'd rather not change the object type due to external dependencies.

Convert any image format to JPG

I have a web app that runs on Azure. The user can upload an image and I save that image for them. Now when I receive the image, I have it as byte[]. I would like to save it as JPG to save space. The source image could be anything such as PNG, BMP or JPG.
Is it possible to do so? This needs to run on Azure and I am using WebApps/MVC5/C#.
Thanks for any help.
Get the memorystream and then use System.Drawing
var stream = new MemoryStream(byteArray)
Image img = new Bitmap(stream);
img.Save(#"c:\s\pic.png", System.Drawing.Imaging.ImageFormat.Png);
The last line there where you save the file, you can select the format.
So the answers by #mjwills and Cody was correct. I still thought to put the two methods I exactly needed:
public static Image BitmapToBytes(byte[] image, ImageFormat pFormat)
{
var imageObject = new Bitmap(new MemoryStream(image));
var stream = new MemoryStream();
imageObject.Save(stream, pFormat);
return new Bitmap(stream);
}
Also:
public static byte[] ImgToByteArray(Image img)
{
using (MemoryStream mStream = new MemoryStream())
{
img.Save(mStream, img.RawFormat);
return mStream.ToArray();
}
}
Cheers everyone.

Reading gif file from base64 into picturebox any other way? C#

I Have gif image in base64.
Currently I am approaching this way. reading base64 gif file and writing it to byte array and writing it back to image file to disk and reading from the file to picturebox.image.
byte[] imageBytes = Convert.FromBase64String(body);
//* this is write file to disk and read
string filename = Username;
File.WriteAllBytes(filename, imageBytes);
fs = new FileStream(filename, FileMode.Open, FileAccess.Read);
pictureBox1.Image = Image.FromStream(fs);
Now, I want to write it to memory without writing it to disk file. like in a form of variable image. that can be assigned to picturebox. Is there any possible of this. because I have to do repeatedly many times for many images.
So I would like find a different approach without writing saving file to disk and reading it again.
Any help appreciated.
byte[] imageBytes = Convert.FromBase64String(body);
MemoryStream stream = new MemoryStream(imageBytes);
pictureBox1.Image = Image.FromStream(stream);
byte[] imageBytes = Convert.FromBase64String(body);
using (var ms = new MemoryStream(imageBytes))
{
pictureBox1.Image = Image.FromStream(ms);
}
Be aware the MemoryStream class is IDisposable so you should Dispose() it. This can happen with using or with try/catch/finally block.

Silverlight 4: How can I convert bmp byte array to png byte array?

I have a wcf service which returns a bmp in byte[]. However Silverlight's Image control doesnt support displaying bmp's so i need to convert the bmp byte[] to png or jpg byte[]. Is there a library out there which does this conversion? Or any other way of displaying the bmp byte[] on the silverlight client?
Thanks!
Update1
In order to achieve the conversion I would have done something like this in .NET
private byte[] ConvertBmpToJpeg(byte[] bmp)
{
using (System.Drawing.Image image = System.Drawing.Image.FromStream(new MemoryStream(bmp)))
{
MemoryStream ms = new MemoryStream();
image.Save(ms, ImageFormat.Jpeg);
return ms.ToArray();
}
}
Since System.Drawing is not available in Silverlight, how do I achieve what the code does above in Silverlight?
Answer
using the library mentioned by dj kraze below-
ExtendedImage img = new ExtendedImage();
var bd = new BmpDecoder();
var je = new JpegEncoder();
bd.Decode(img, new MemoryStream(bitmapBytes));
MemoryStream ms = new MemoryStream();
je.Encode(img, ms);
BitmapImage bi = new BitmapImage();
bi.SetSource(new MemoryStream(ms.ToArray()));
display_ScreenShot.Source = bi;
Here is an even easier way of doing it..
This site may help out a lot
Image Converting

Parameter is not valid error when creating image from byte[] in c#

I am trying to convert a byte[] to Bitmap in c#. Following is the code:
MemoryStream ms = new MemoryStream(b);
Bitmap bmp = new Bitmap(ms);
It shows the error Parameter is not valid when creating the Bitmap.
byte[] b is coming from a network stream.
But when I write this byte[] to a file, and open this file in any image viewer just works perfectly. Following is code for writing the byte[] to file:
var fs = new BinaryWriter(new FileStream("tmp.bmp", FileMode.Create, FileAccess.Write));
fs.Write(b);
fs.Close();
What am I missing here?
EDIT
Here is my full code that was causing problem
Socket s = listener.AcceptSocket();
byte[] b = new byte[imgLen];
s.Receive(b);
MemoryStream ms = new MemoryStream(b);
// now here I am using ms.Seek(0, SeekOrigin.Begin); that fixed my problem.
Bitmap bmp = new Bitmap(ms);
pictureBox1.Image = bmp;
s.Close();
I am using this code on Form_Load event and there is nothing extra. I am just trying to display an Image that is streamed on network. The server is written in Java that is streaming this image.
Hope it clarifies the doubts.
Thanks
Okay, just to clarify things a bit... the problem is that new Bitmap(ms) is going to read the data from the stream's current position - if the stream is currently positioned at the end of the data, it's not going to be able to read anything, hence the problem.
The question claims that the code is this:
MemoryStream ms = new MemoryStream(b);
Bitmap bmp = new Bitmap(ms);
In that case there is no requirement to reset the position of the stream, as it will be 0 already. However, I suspect the code is actually more like this:
MemoryStream ms = new MemoryStream();
// Copy data into ms here, e.g. reading from NetworkStream
Bitmap bmp = new Bitmap(ms);
or possibly:
MemoryStream ms = new MemoryStream(b);
// Other code which *reads* from ms, which will change its position,
// before we finally call the constructor:
Bitmap bmp = new Bitmap(ms);
In this case you do need to reset the position, because otherwise the "cursor" of the stream is at the end of the data instead of the start. Personally, however, I prefer using the Position property instead of the Seek method, just for simplicity, so I'd use:
MemoryStream ms = new MemoryStream();
// Copy data into ms here, e.g. reading from NetworkStream
// Rewind the stream ready for reading
ms.Position = 0;
Bitmap bmp = new Bitmap(ms);
It just goes to show how important it is that the sample code in a question is representative of the actual code...
Try resetting current location in the stream
MemoryStream ms = new MemoryStream(b);
ms.Seek(0, SeekOrigin.Begin);
Bitmap bmp = new Bitmap(ms);
Try like this:
byte[] b = ...
using (var ms = new MemoryStream(b))
using (var bmp = Image.FromStream(ms))
{
// do something with the bitmap
}
Error is shown if you are disposing the image. Try removing that from code

Categories