I have an file name data.yuv ( It's contain image data in YUV format).
What I need to do to convert the file to jpg or bmp image with C# ?
If I save the yuv file to image. It's show an green image. Thus, I need codes to convert data from yuv to image.
Open that file by using a FileStream and use the constructor of Bitmap to convert the stream to a valid bitmap.
FileStream fs = new FileStream("somefile.yuv", FileMode.Open);
Bitmap bmp = new Bitmap(fs);
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 am saving image into database as binary format, and while retrieving i am converting that binary format into image file and displaying.
byte[] picbin = (byte[])(binary data in datbase);
ImageConverter ic = new ImageConverter();
System.Drawing.Image img = (System.Drawing.Image)ic.ConvertFrom(picbin);`
and saving img- variable into some folder, by giving some name. but now i need to able to provide "downloadable option" to the user for that particular image in any one format, so that user can verify it so what is the way to implement this.
To convert image to different format:
// Save the image in JPEG format.
img.Save(#"C:\test.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
// Save the image in GIF format.
img.Save(#"C:\test.gif", System.Drawing.Imaging.ImageFormat.Gif);
// Save the image in PNG format.
img.Save(#"C:\test.png", System.Drawing.Imaging.ImageFormat.Png);
Check How to: Convert Images from One Format to Another
I have a gray-scale TIFF image which windows properties say its Bit-depth is 4 (which is supposed to mean it is 4 BPP), but when I open the image in C# as a bitmap the pixelFormat property says it is Format8bppIndexed (8 BPP), is it the bitmap constructor changing the pixel format or I misunderstood something?
The TIFF format is an container (like ZIP). One TIFF file can contain various frames containing different files / data. An frame can be a bitmap. And each bitmap can have a different pixelFormat. A TIFF file can (or could) have a preview bitmap that has a different pixelFormat.
To get the actual pixelformat of the first frame you could use this :
Stream imageStreamSource = new FileStream("file.tif", FileMode.Open, FileAccess.Read, FileShare.Read);
TiffBitmapDecoder decoder = new TiffBitmapDecoder(imageStreamSource, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
BitmapSource bitmapSource = decoder.Frames[0];
PixelFormat pixelFormat = bitmapSource.Format;
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.
is there anyway that I can convert a png to a bmp in C#?
I want to download a image then convert it to a bmp then set it as the desktop background.
I have the downloading bit and the background bit done.
I just need to convert the png to a bmp.
Image Dummy = Image.FromFile("image.png");
Dummy.Save("image.bmp", ImageFormat.Bmp);
Certainly. You'd want to load up a Bitmap object with your png:
Bitmap myBitmap = new Bitmap("mypng.png");
Then save it:
myBitmap.Save("mybmp.bmp", System.Drawing.Imaging.ImageFormat.Bmp);
Have you tried this?
Image imgFile = Image.FromFile(aFileName);
imgFile .Save(strOutFileName, ImageFormat.Bmp);