How to convert byte[] of PNG into jpeg - c#

I have a byte[] of image data (of png type) and I want to convert it into jpeg type image. What would be the best way of doing it in Windows Phone 8?

Try this:
using(var stream = new MemoryStream(bytes))
{
//bytes is byte[] containing the image data
var image = Image.FromStream(stream);
image.Save(File.OpenWrite("MyImage.jpeg", ImageFormat.Jpeg);
}

Related

C# Image AS3 ByteArray

I'm looking for the conventer from normal image in C# to AMF3 ByteArray. The image format is JPG, I'm using FluorineFX library to serialize and de-serliazize AMF3 Data.
I need to get image ByteArray in C# from JPG because I'm using this to my flash game, and I don't know how to serialize image to AMF3 ByteArray. There isn't much info on FluorineFX neither AMF3 C# ByteArray.
According to old FluorineFX Documentation if you want to convert image to byte array you need to use byte[] (variable types ending with [] are arrays) or FluorineFx.AMF3.ByteArray.
byte[] Example code:
public byte[] imageToByteArray(System.Drawing.Image imageIn)
{
MemoryStream ms = new MemoryStream();
imageIn.Save(ms,System.Drawing.Imaging.ImageFormat.Gif);
return ms.ToArray();
}
public Image byteArrayToImage(byte[] byteArrayIn)
{
MemoryStream ms = new MemoryStream(byteArrayIn);
Image returnImage = Image.FromStream(ms);
return returnImage;
}

Getting an image path to use in converting to base 64 string

I have an image in my images folder
and I want to pass this image to an Image object so I can then convert it to a base 64 string. This will then be passed to the client and inserted into the src of a img element.
The problem is that I don't know how to get the image from disk into the image object.
Image img = Image.FromFile(#"..Images\no_image.jpg"); // doesn't work
string image = Convert.ToBase64String(imageToByteArray(img));
public byte[] imageToByteArray(Image imageIn)
{
MemoryStream ms = new MemoryStream();
imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
return ms.ToArray();
}
If you work on a web project, you should use:
Image.FromFile(Server.MapPath("~/Images/no_image.jpg"))
else if it is a WinForm/WPF/Console application, use:
Image.FromFile(System.IO.Path.Combine(Environment.CurrentDirectory, "Images", "no_image.jpg"))

How to Convert and display only the first page from Multipage Blob .Tiff image to .Jpeg using C#

I am pretty much newer in asp.net MVC. My core objective is to Convert the .tiff image to .jpeg format without saving as file. For that i call separate file(ex:gettiff.aspx) for showing .tiff image in browser.
What i have done?
I was saved the multipage .tiff image into DB as blob.
What i need?
Need to show that multipage .tiff image by converting to .Jpeg in View
What i successfully done?
To convert single page .tiff image(File/Blob), i don't have any problem in conversion as .jpeg and viewed as well in VIEW
Where i stuck?
To convert multipage .tiff image(File/Blob), i have problem during the conversion as .Jpeg and viewed as well in VIEW
Sample Code:
C#: In gettiff.aspx file
while (Reader.Read())
{
Bitmap bImage = ByteToImage((byte[])Reader["refImage"]);
byte[] array1 = ImageToByte2(myBmp);
Response.ContentType = "image/jpeg"; // if your image is a jpeg of course
Response.BinaryWrite((byte[])array1);
}
Convert Blob to Bitmap:
public static Bitmap ByteToImage(byte[] blob)
{
using (MemoryStream mStream = new MemoryStream())
{
mStream.Write(blob, 0, blob.Length);
mStream.Seek(0, SeekOrigin.Begin);
Bitmap bm = new Bitmap(mStream);
return bm;
}
}
Convert bitmap to bytearray (this will gives as .jpeg format)
public static byte[] ImageToByte2(Bitmap img)
{
byte[] byteArray = new byte[0];
using (MemoryStream stream = new MemoryStream())
{
//System.Drawing.Image.FromStream(stream);
img.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
byteArray = stream.ToArray();
}
return byteArray;
}
In View:
<img src="../GetTiffImage.aspx?batchImageId=#item.RefImgId&&ImgType=CBlob" width="150" height="100" style=" border:groove;border-color: #ff0000;" />
I need to convert as .jpeg and display only the first page from that multi page .tiff blob.
Problem occurred while saving the stream - "A generic error occurred in GDI+"
Anybody can help me...
I Found Solution here. By getting that single line code i can able to display .tiff blob image
Sample Code:
public static Bitmap ByteToImage(byte[] blob)
{
Bitmap bm = (Bitmap)((new ImageConverter()).ConvertFrom(blob));
return bm;
}
Instead of using Memory Stream, if i use this conversion will able to get the image in View. Remaining codes are same from the question

Convert System.Drawing.Image to System.Windows.Controls.Image?

Is there a way in C# to do this conversion and back?
I have a WPF app which has a Image control. I'm trying to save the image in that control to a SQL Database.
In my Entity Model, the datatype of the picture column in my database is a byte[]. So I found a method to convert a System.Drawing.Image to a byte[] and back. But I haven't found a method to convert from System.Windows.Controls.Image to a byte[].
So that's why I now need to do the above conversion.
If you have a byte array that represents a file that WPF can decode (bmp, jpg, gif, png, tif, ico), you can do the following:
BitmapSource LoadImage(Byte[] imageData)
{
using (MemoryStream ms = new MemoryStream(imageData))
{
var decoder = BitmapDecoder.Create(ms,
BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad);
return decoder.Frames[0];
}
}
Likewise, to convert it back, you can do the following:
byte[] SaveImage(BitmapSource bitmap)
{
using (MemoryStream ms = new MemoryStream())
{
var encoder = new BmpBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(bitmap));
encoder.Save(ms);
return ms.GetBuffer();
}
}
Well, one is an image and one is a control that shows an image, so I don't think that there's a conversion between the two. But, you could set the Source of the ...Controls.Image to be your ...Drawing.Image.
Edit based on update
Does this do what you need - http://msdn.microsoft.com/en-us/library/ms233764%28VS.100%29.aspx

C#: How to convert BITMAP byte array to JPEG format?

How can I convert a BITMAP in byte array format to JPEG format using .net 2.0?
What type of byte[] do you mean? The raw file-stream data? In which case, how about something like (using System.Drawing.dll in a client application):
using(Image img = Image.FromFile("foo.bmp"))
{
img.Save("foo.jpg", ImageFormat.Jpeg);
}
Or use FromStream with a new MemoryStream(arr) if you really do have a byte[]:
byte[] raw = ...todo // File.ReadAllBytes("foo.bmp");
using(Image img = Image.FromStream(new MemoryStream(raw)))
{
img.Save("foo.jpg", ImageFormat.Jpeg);
}
If it is just a buffer of raw pixel data, and not a complete image file(including headers etc., such as a JPEG) then you can't use Image.FromStream.
I think what you might be looking for is System.Drawing.Bitmap.LockBits, returning a System.Drawing.Imaging.ImageData; this provides access to reading and writing the image's pixels using a pointer to memory.
public static Bitmap BytesToBitmap(byte[] byteArray)
{
using (MemoryStream ms = new MemoryStream(byteArray))
{
Bitmap img = (Bitmap)Image.FromStream(ms);
return img;
}
}

Categories

Resources