将8bpp avi转换为16bpp avi [英] Convert 8bpp avi into 16bpp avi

查看:106
本文介绍了将8bpp avi转换为16bpp avi的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿!我需要将8 bpp avi文件转换为16 bpp avi文件.

有人知道怎么做吗?

我使用此代码从avi文件获取位图图像.它适用于16 bpp或更高的速度,但不适用于8bpp avi文件.

公共位图GetBitmap(int位置){
if(position> countFrames){
抛出新的Exception("Invalid frame position:" + position);
}

Avi.AVISTREAMINFO streamInfo = GetStreamInfo(StreamPointer);

//解压缩框架并返回指向DIB的指针
int dib = Avi.AVIStreamGetFrame(getFrameObject,firstFrame + position);
//将位图标头复制到托管结构中
Avi.BITMAPINFOHEADER bih =新的Avi.BITMAPINFOHEADER();
bih =(Avi.BITMAPINFOHEADER)Marshal.PtrToStructure(new IntPtr(dib),bih.GetType());

if(bih.biSizeImage< 1){
抛出新的Exception("VideoStreamGetFrame中的Exception");
}

//复制图片

byte [] bitmapData;
int地址= dib + Marshal.SizeOf(bih);


位图saveableBitmap;

如果(bih.biBitCount< 16)
{
//复制调色板和像素
//位图b =(Bitmap)Image.FromStream(aviStream);
bitmapData =新字节[bih.biSizeImage + Avi.PALETTE_SIZE];


}
其他
{
//仅复制像素
bitmapData =新字节[bih.biSizeImage];
}

Marshal.Copy(新IntPtr(地址),bitmapData,0,bitmapData.Length);

//复制位图信息
byte [] bitmapInfo =新的byte [Marshal.SizeOf(bih)];
IntPtr ptr;
ptr = Marshal.AllocHGlobal(bitmapInfo.Length);
元帅.StructureToPtr(bih,ptr,false);
地址= ptr.ToInt32();
Marshal.Copy(新IntPtr(地址),bitmapInfo,0,bitmapInfo.Length);

元帅FreeHGlobal(ptr);

//创建文件头文件
Avi.BITMAPFILEHEADER bfh =新的Avi.BITMAPFILEHEADER();
bfh.bfType = Avi.BMP_MAGIC_COOKIE;
bfh.bfSize =(Int32)(55 + bih.biSizeImage); //写入磁盘的文件大小
bfh.bfReserved1 = 0;
bfh.bfReserved2 = 0;
bfh.bfOffBits = Marshal.SizeOf(bih)+ Marshal.SizeOf(bfh);
如果(bih.biBitCount< 16)
{
//标题和像素数据之间有一个调色板
bfh.bfOffBits + = Avi.PALETTE_SIZE;
}

//编写位图流
BinaryWriter bw = new BinaryWriter(new MemoryStream());

//写标题
bw.Write(bfh.bfType);
bw.Write(bfh.bfSize);
bw.Write(bfh.bfReserved1);
bw.Write(bfh.bfReserved2);
bw.Write(bfh.bfOffBits);
//写入位图信息
bw.Write(bitmapInfo);
//写入位图数据
bw.Write(bitmapData);

位图bmp =(Bitmap)Image.FromStream(bw.BaseStream);
saveableBitmap =新的位图(bmp.Width,bmp.Height);
图形g = Graphics.FromImage(saveableBitmap);
g.DrawImage(bmp,0,0);
g.Dispose();
bmp.Dispose();

bw.Close();

返回saveableBitmap;
}




请帮助我,我是C#的新手.

在此先感谢.

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.

推荐答案

我不知道转换它们的方法.使用DexterLib从avi获取帧.
I don''t know of a way to convert them. Use DexterLib to get frames from an avi.


这篇关于将8bpp avi转换为16bpp avi的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆