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.
Related
I am trying to create an EMF image from a MS Chart graph. I have got no problem using a FileStream to temporary store the image like this:
FileStream fileStream = new FileStream("test.emf", FileMode.Create);
chart1.SaveImage(fileStream, ChartImageFormat.EmfPlus);
fileStream.Close();
Image emf = Metafile.FromFile("test.emf");
But if I try to get rid of the file using a MemoryStream I get a System.ArgumentException
HResult=0x80070057 (invalid parameter).
MemoryStream memoryStream = new MemoryStream();
chart1.SaveImage(memoryStream, ChartImageFormat.EmfPlus);
Image emf = Metafile.FromStream(memoryStream);
I can't figure out why it is not working. I've read that the problem may be caused by the need to dispose the Graphics object. How can do it?
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;
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);
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
I am using this C# code to access an image file in order to read metadata from it.
BitmapSource img = BitmapFrame.Create(uri);
Unfortunately the image file specified by uri becomes locked until the program ends. How do I prevent the image from being locked?
maybe this could help ?
edit
BitmapSource img = BitmapFrame.Create(uri,BitmapCreateOptions.None,BitmapCacheOption.OnLoad);
BitmapCreateOptions.None = default option
BitmapCacheOption.OnLoad = Caches the entire image into memory at load time. All requests for image data are filled from the memory store.
from here
If you want to be able to delete/change the file immediately afterwards, read the whole file into memory, and then give it the MemoryStream instead. For example:
MemoryStream data = new MemoryStream(File.ReadAllBytes(file));
BitmapSource bitmap = BitmapFrame.Create(data);
You can also use generic stream:
Stream stream = File.OpenRead(filename);
Bitmap template = new Bitmap(stream); // or (Bitmap) Bitmap.FromStream(stream)
stream.Close();