Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.
Posted
Updated 20-Jul-11 0:24am
v2
Comments
ankur.mu 20-Jul-11 5:06am    
If anyone knows to read 8bpp avi files & split them into bmp images. PLZ help me out.

1 solution

I don't know of a way to convert them. Use DexterLib to get frames from an avi.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900