Hey! I need to convert 8 bpp avi file into 16 bpp avi file.
Does anyone know how to do it?
I used this code to get bitmap images from avi file. It works fine with 16 bpp or more but does not work for 8bpp avi files properly.
public Bitmap GetBitmap(int position){
if(position > countFrames){
throw new Exception("Invalid frame position: "+position);
}
Avi.AVISTREAMINFO streamInfo = GetStreamInfo(StreamPointer);
//Decompress the frame and return a pointer to the DIB
int dib = Avi.AVIStreamGetFrame(getFrameObject, firstFrame + position);
//Copy the bitmap header into a managed struct
Avi.BITMAPINFOHEADER bih = new Avi.BITMAPINFOHEADER();
bih = (Avi.BITMAPINFOHEADER)Marshal.PtrToStructure(new IntPtr(dib), bih.GetType());
if(bih.biSizeImage < 1){
throw new Exception("Exception in VideoStreamGetFrame");
}
//copy the image
byte[] bitmapData;
int address = dib + Marshal.SizeOf(bih);
Bitmap saveableBitmap;
if (bih.biBitCount < 16)
{
//copy palette and pixels
// Bitmap b = (Bitmap)Image.FromStream(aviStream);
bitmapData = new byte[bih.biSizeImage + Avi.PALETTE_SIZE];
}
else
{
//copy only pixels
bitmapData = new byte[bih.biSizeImage];
}
Marshal.Copy(new IntPtr(address), bitmapData, 0, bitmapData.Length);
//copy bitmap info
byte[] bitmapInfo = new byte[Marshal.SizeOf(bih)];
IntPtr ptr;
ptr = Marshal.AllocHGlobal(bitmapInfo.Length);
Marshal.StructureToPtr(bih, ptr, false);
address = ptr.ToInt32();
Marshal.Copy(new IntPtr(address), bitmapInfo, 0, bitmapInfo.Length);
Marshal.FreeHGlobal(ptr);
//create file header
Avi.BITMAPFILEHEADER bfh = new Avi.BITMAPFILEHEADER();
bfh.bfType = Avi.BMP_MAGIC_COOKIE;
bfh.bfSize = (Int32)(55 + bih.biSizeImage); //size of file as written to disk
bfh.bfReserved1 = 0;
bfh.bfReserved2 = 0;
bfh.bfOffBits = Marshal.SizeOf(bih) + Marshal.SizeOf(bfh);
if (bih.biBitCount < 16)
{
//There is a palette between header and pixel data
bfh.bfOffBits += Avi.PALETTE_SIZE;
}
//write a bitmap stream
BinaryWriter bw = new BinaryWriter(new MemoryStream());
//write header
bw.Write(bfh.bfType);
bw.Write(bfh.bfSize);
bw.Write(bfh.bfReserved1);
bw.Write(bfh.bfReserved2);
bw.Write(bfh.bfOffBits);
//write bitmap info
bw.Write(bitmapInfo);
//write bitmap data
bw.Write(bitmapData);
Bitmap bmp = (Bitmap)Image.FromStream(bw.BaseStream);
saveableBitmap = new Bitmap(bmp.Width, bmp.Height);
Graphics g = Graphics.FromImage(saveableBitmap);
g.DrawImage(bmp, 0, 0);
g.Dispose();
bmp.Dispose();
bw.Close();
return saveableBitmap;
}
Please help me I am new to C#.
Thanks in Advance.