[Solved - Kinda] I've created 2 images, 1 is 48bpp and the other is 64bpp. Reading these images with ffmpeg, ffmpeg output is.
Here is output from the 48bpp png file.
Stream #0:0: Video: png, rgb48be(pc, gbr/bt709/iec61966-2-1), 1920x1200 [SAR 3779:3779 DAR 8:5], 25 fps, 25 tbr, 25 tbn
Here is output from the 64bpp png file.
Stream #0:0: Video: png, rgba64be(pc, gbr/bt709/iec61966-2-1), 1027x742 [SAR 3779:3779 DAR 1027:742], 25 fps, 25 tbr, 25 tbn
When I load the png files with the code below, I always recieve output that the PixelFormat is Format32bppArgb.
This is the code used to load the images.
using Stream stream = File.OpenRead(imagePath);
Bitmap bitmap = (Bitmap)Image.FromStream(stream);
Console.WriteLine(bitmap.PixelFormat);
Why are the images not not being read correctly?
Update : The images were created using the bitmap class, If the bitmap class saved these images, shouldn't it be able to read the images that it created?
I've created a work-around (less than ideal because it produces larger files since it stores the actual pixel Data). First, we write the Width, Height and PixelFormat(as an int value). I call this the header(12 bytes) then the pixelData. When we read this byte array back we read the header info then seperate the header from the pixelData. Below is the two methods to read and write the image back and forth.
public static void WriteImageDataToFile(Bitmap bitmap, string filePath)
{
int width = bitmap.Width;
int height = bitmap.Height;
PixelFormat pixelFormat = bitmap.PixelFormat;
ImageLockMode lockMode = ImageLockMode.ReadOnly;
Rectangle rect = new(0, 0, width, height);
BitmapData bitmapData = bitmap.LockBits(rect, lockMode, pixelFormat);
int pixelDataSize = bitmapData.Stride * bitmapData.Height;
byte[] pixelData = new byte[pixelDataSize];
Marshal.Copy(bitmapData.Scan0, pixelData, 0, pixelDataSize);
bitmap.UnlockBits(bitmapData);
using Stream stream = File.Create(filePath);
stream.Write(BitConverter.GetBytes(width));
stream.Write(BitConverter.GetBytes(height));
stream.Write(BitConverter.GetBytes((int)pixelFormat));
stream.Write(pixelData);
}
public static Bitmap ReadImageDataFromFile(string filePath)
{
int headerSize = 12;
byte[] rawData = File.ReadAllBytes(filePath);
int pixelSize = rawData.Length - headerSize;
int width = BitConverter.ToInt32(rawData, 0);
int height = BitConverter.ToInt32(rawData, 4);
int pixelFormat = BitConverter.ToInt32(rawData, 8);
byte[] pixelData = new byte[pixelSize];
Array.Copy(rawData, headerSize, pixelData, 0, pixelSize);
Bitmap bitmap = new(width, height, (PixelFormat)pixelFormat);
Rectangle rect = new(0, 0, bitmap.Width, bitmap.Height);
ImageLockMode lockMode = ImageLockMode.WriteOnly;
BitmapData bitmapData = bitmap.LockBits(rect, lockMode, (PixelFormat)pixelFormat);
Marshal.Copy(rawData, 0, bitmapData.Scan0, pixelSize);
bitmap.UnlockBits(bitmapData);
return bitmap;
}
What I have tried:
Have tried loading the images from stream and from file.