本来的代码是固定只能使用设定好的BMP文件来转AVI
void CAVIGenDlg::OnBnClickedButton1()
{
// TODO: Add your control notification handler code here
CAVIFile avi("bmp2avi.avi"); for (int i=1; i<=20; ++i)
{
CBitmap bmp;
// fill with data
char name[50];
wsprintf(name, TEXT("bmp\\StillCap%d.bmp\0"), i);
::AfxMessageBox(name);
HBITMAP hbitmap=(HBITMAP)::LoadImage (::AfxGetInstanceHandle(),name,IMAGE_BITMAP,0,0, LR_LOADFROMFILE|LR_CREATEDIBSECTION);
bmp.Attach(hbitmap); avi.AddFrame(bmp);
} if (avi.IsOK())
::AfxMessageBox("AVI created");
else
::AfxMessageBox("Error");
}然后我参考了网上的代码想改成把指定文件夹下BMP图片合成AVI视频的void CAVIGenDlg::OnBnClickedButton1()
{
::AfxMessageBox("Please put your bmp files into the Folder \"bmp\".");
CAVIFile avi("bmp2avi.avi");
CString picadd[999];
int picnum=0;
CFileFind FileFind;
BOOL Ret = FALSE;
CString FileName;
CString str;
::SetCurrentDirectory("bmp");
Ret = FileFind.FindFile(NULL, 0);
while(Ret)
{
Ret = FileFind.FindNextFile();
FileName = FileFind.GetFileName();
if(FileName!="." && FileName!="..")
{
if(FileName[FileName.StringLength(FileName)-1]=='p'){
picadd[picnum]=FileName;
picnum++;}
}
}
for(int i=0;i<picnum;i++)
{
str+=picadd[i]+"111";
}
MessageBox(str);
for (int i=0; i<picnum; ++i)
{
CBitmap bmp;
// fill with data
char name[50];
wsprintf(name, TEXT("bmp\\%s\0"),picadd[i]);
::AfxMessageBox(name);
HBITMAP hbitmap=(HBITMAP)::LoadImage (::AfxGetInstanceHandle(),name,
IMAGE_BITMAP,0,0,
LR_LOADFROMFILE|LR_CREATEDIBSECTION);
bmp.Attach(hbitmap); avi.AddFrame(bmp);
}
if (avi.IsOK())
::AfxMessageBox("The AVI video has been created.");
else
::AfxMessageBox("Error.please try again."); 但是修改以后文件在转换成AVI视频中出了错
提示是在标记error2的地方 bool CAVIFile::AddFrame(CBitmap& bmp)
{
HRESULT hr;
char szMessage[BUFSIZE];
if (!bOK){ ::AfxMessageBox("Error1");
return false; }
LPBITMAPINFOHEADER alpbi = (LPBITMAPINFOHEADER)GlobalLock(MakeDib(bmp, 24));
if (alpbi == NULL) { ::AfxMessageBox("Error2");
return false; }
if (xDim>=0 && xDim != alpbi->biWidth)
{
GlobalFreePtr(alpbi); ::AfxMessageBox("Error3");
return false;
}
if (yDim>=0 && yDim != alpbi->biHeight)
{
GlobalFreePtr(alpbi);::AfxMessageBox("Error4");
return false;
}
xDim = alpbi->biWidth;
yDim = alpbi->biHeight;
if (nFrames == 0)
{
hr = AVIFileOpen(
&pfile, // returned file pointer
FName, // file name
OF_WRITE | OF_CREATE, // mode to open file with
NULL); // use handler determined
// from file extension....
if (hr != AVIERR_OK)
{
GlobalFreePtr(alpbi);
bOK = false; ::AfxMessageBox("Error5");
return false;
}
_fmemset(&strhdr, 0, sizeof(strhdr));
strhdr.fccType = streamtypeVIDEO;// stream type
strhdr.fccHandler = 0;
strhdr.dwScale = 1;
strhdr.dwRate = 15; // 15 fps
strhdr.dwSuggestedBufferSize = alpbi->biSizeImage;
SetRect(&strhdr.rcFrame, 0, 0, // rectangle for stream
(int) alpbi->biWidth,
(int) alpbi->biHeight);
// And create the stream;
hr = AVIFileCreateStream(pfile, // file pointer
&ps, // returned stream pointer
&strhdr); // stream header
if (hr != AVIERR_OK)
{
GlobalFreePtr(alpbi);
bOK = false; ::AfxMessageBox("Error6");
return false;
}
_fmemset(&opts, 0, sizeof(opts));
if (!AVISaveOptions(NULL, 0, 1, &ps, (LPAVICOMPRESSOPTIONS FAR *) &aopts))
{
GlobalFreePtr(alpbi);
bOK = false; ::AfxMessageBox("Error7");
return false;
}
hr = AVIMakeCompressedStream(&psCompressed, ps, &opts, NULL);
if (hr != AVIERR_OK)
{
GlobalFreePtr(alpbi);
bOK = false; ::AfxMessageBox("Error8");
return false;
}
hr = AVIStreamSetFormat(psCompressed, 0,
alpbi, // stream format
alpbi->biSize + // format size
alpbi->biClrUsed * sizeof(RGBQUAD));
if (hr != AVIERR_OK)
{
GlobalFreePtr(alpbi);
bOK = false; ::AfxMessageBox("Error9");
return false;
}
// Fill in the stream header for the text stream....
// The text stream is in 60ths of a second....
}
// Jetzt eigentliches Schreiben
hr = AVIStreamWrite(psCompressed, // stream pointer
nFrames * 10, // time of this frame
1, // number to write
(LPBYTE) alpbi + // pointer to data
alpbi->biSize +
alpbi->biClrUsed * sizeof(RGBQUAD),
alpbi->biSizeImage, // size of this frame
AVIIF_KEYFRAME, // flags....
NULL,
NULL);
if (hr != AVIERR_OK)
{
GlobalFreePtr(alpbi);
bOK = false; ::AfxMessageBox("Error10");
return false;
}
GlobalFreePtr(alpbi);
nFrames++;
return true;
} 但如果吧LPBITMAPINFOHEADER alpbi = (LPBITMAPINFOHEADER)GlobalLock(MakeDib(bmp, 24)); 中的24改16或者32,则这里可以通过但是会在error9处报错。不知道是不是文件上出现什么问题,还请指教
void CAVIGenDlg::OnBnClickedButton1()
{
// TODO: Add your control notification handler code here
CAVIFile avi("bmp2avi.avi"); for (int i=1; i<=20; ++i)
{
CBitmap bmp;
// fill with data
char name[50];
wsprintf(name, TEXT("bmp\\StillCap%d.bmp\0"), i);
::AfxMessageBox(name);
HBITMAP hbitmap=(HBITMAP)::LoadImage (::AfxGetInstanceHandle(),name,IMAGE_BITMAP,0,0, LR_LOADFROMFILE|LR_CREATEDIBSECTION);
bmp.Attach(hbitmap); avi.AddFrame(bmp);
} if (avi.IsOK())
::AfxMessageBox("AVI created");
else
::AfxMessageBox("Error");
}然后我参考了网上的代码想改成把指定文件夹下BMP图片合成AVI视频的void CAVIGenDlg::OnBnClickedButton1()
{
::AfxMessageBox("Please put your bmp files into the Folder \"bmp\".");
CAVIFile avi("bmp2avi.avi");
CString picadd[999];
int picnum=0;
CFileFind FileFind;
BOOL Ret = FALSE;
CString FileName;
CString str;
::SetCurrentDirectory("bmp");
Ret = FileFind.FindFile(NULL, 0);
while(Ret)
{
Ret = FileFind.FindNextFile();
FileName = FileFind.GetFileName();
if(FileName!="." && FileName!="..")
{
if(FileName[FileName.StringLength(FileName)-1]=='p'){
picadd[picnum]=FileName;
picnum++;}
}
}
for(int i=0;i<picnum;i++)
{
str+=picadd[i]+"111";
}
MessageBox(str);
for (int i=0; i<picnum; ++i)
{
CBitmap bmp;
// fill with data
char name[50];
wsprintf(name, TEXT("bmp\\%s\0"),picadd[i]);
::AfxMessageBox(name);
HBITMAP hbitmap=(HBITMAP)::LoadImage (::AfxGetInstanceHandle(),name,
IMAGE_BITMAP,0,0,
LR_LOADFROMFILE|LR_CREATEDIBSECTION);
bmp.Attach(hbitmap); avi.AddFrame(bmp);
}
if (avi.IsOK())
::AfxMessageBox("The AVI video has been created.");
else
::AfxMessageBox("Error.please try again."); 但是修改以后文件在转换成AVI视频中出了错
提示是在标记error2的地方 bool CAVIFile::AddFrame(CBitmap& bmp)
{
HRESULT hr;
char szMessage[BUFSIZE];
if (!bOK){ ::AfxMessageBox("Error1");
return false; }
LPBITMAPINFOHEADER alpbi = (LPBITMAPINFOHEADER)GlobalLock(MakeDib(bmp, 24));
if (alpbi == NULL) { ::AfxMessageBox("Error2");
return false; }
if (xDim>=0 && xDim != alpbi->biWidth)
{
GlobalFreePtr(alpbi); ::AfxMessageBox("Error3");
return false;
}
if (yDim>=0 && yDim != alpbi->biHeight)
{
GlobalFreePtr(alpbi);::AfxMessageBox("Error4");
return false;
}
xDim = alpbi->biWidth;
yDim = alpbi->biHeight;
if (nFrames == 0)
{
hr = AVIFileOpen(
&pfile, // returned file pointer
FName, // file name
OF_WRITE | OF_CREATE, // mode to open file with
NULL); // use handler determined
// from file extension....
if (hr != AVIERR_OK)
{
GlobalFreePtr(alpbi);
bOK = false; ::AfxMessageBox("Error5");
return false;
}
_fmemset(&strhdr, 0, sizeof(strhdr));
strhdr.fccType = streamtypeVIDEO;// stream type
strhdr.fccHandler = 0;
strhdr.dwScale = 1;
strhdr.dwRate = 15; // 15 fps
strhdr.dwSuggestedBufferSize = alpbi->biSizeImage;
SetRect(&strhdr.rcFrame, 0, 0, // rectangle for stream
(int) alpbi->biWidth,
(int) alpbi->biHeight);
// And create the stream;
hr = AVIFileCreateStream(pfile, // file pointer
&ps, // returned stream pointer
&strhdr); // stream header
if (hr != AVIERR_OK)
{
GlobalFreePtr(alpbi);
bOK = false; ::AfxMessageBox("Error6");
return false;
}
_fmemset(&opts, 0, sizeof(opts));
if (!AVISaveOptions(NULL, 0, 1, &ps, (LPAVICOMPRESSOPTIONS FAR *) &aopts))
{
GlobalFreePtr(alpbi);
bOK = false; ::AfxMessageBox("Error7");
return false;
}
hr = AVIMakeCompressedStream(&psCompressed, ps, &opts, NULL);
if (hr != AVIERR_OK)
{
GlobalFreePtr(alpbi);
bOK = false; ::AfxMessageBox("Error8");
return false;
}
hr = AVIStreamSetFormat(psCompressed, 0,
alpbi, // stream format
alpbi->biSize + // format size
alpbi->biClrUsed * sizeof(RGBQUAD));
if (hr != AVIERR_OK)
{
GlobalFreePtr(alpbi);
bOK = false; ::AfxMessageBox("Error9");
return false;
}
// Fill in the stream header for the text stream....
// The text stream is in 60ths of a second....
}
// Jetzt eigentliches Schreiben
hr = AVIStreamWrite(psCompressed, // stream pointer
nFrames * 10, // time of this frame
1, // number to write
(LPBYTE) alpbi + // pointer to data
alpbi->biSize +
alpbi->biClrUsed * sizeof(RGBQUAD),
alpbi->biSizeImage, // size of this frame
AVIIF_KEYFRAME, // flags....
NULL,
NULL);
if (hr != AVIERR_OK)
{
GlobalFreePtr(alpbi);
bOK = false; ::AfxMessageBox("Error10");
return false;
}
GlobalFreePtr(alpbi);
nFrames++;
return true;
} 但如果吧LPBITMAPINFOHEADER alpbi = (LPBITMAPINFOHEADER)GlobalLock(MakeDib(bmp, 24)); 中的24改16或者32,则这里可以通过但是会在error9处报错。不知道是不是文件上出现什么问题,还请指教
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货