关于将BMP转化成JPG的问题????

nixinfei 2005-09-29 06:12:50
请问有没有方法可以直接将缓冲区中的BMP图象数据转化成JPG图象格式的数据???
...全文
220 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
xianshiqi 2005-10-02
  • 打赏
  • 举报
回复
网上有很多现成的,
phoenix96_2000 2005-09-30
  • 打赏
  • 举报
回复
CxImage:

CxImage img;
if(img.Load("a.bmp"))
{
img.Save("a.jpg",CXIMAGE_FORMAT_JPG);
}

GDI+:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdicpp/GDIPlus/usingGDIPlus/usingimageencodersanddecoders/convertingabmpimagetoapngimage.asp

这是转换位PNG的例子,把encoder名字改为"image/jpeg"就可以了
gxning 2005-09-30
  • 打赏
  • 举报
回复
//生成Bitmap
AM_MEDIA_TYPE mt;
JIF(hr = pSampleGrabber->GetConnectedMediaType(&mt));
if (FAILED(hr)) return E_FAIL;

VIDEOINFOHEADER *pVideoHeader = (VIDEOINFOHEADER*)mt.pbFormat;
if(pVideoHeader==NULL) return E_FAIL;

BITMAPINFO BitmapInfo;
ZeroMemory(&BitmapInfo, sizeof(BitmapInfo));
CopyMemory(&BitmapInfo.bmiHeader, &(pVideoHeader->bmiHeader), sizeof(BITMAPINFOHEADER));

HBITMAP hBitmap;
hBitmap = ::CreateDIBitmap(::GetDC(NULL), &(pVideoHeader->bmiHeader), CBM_INIT, pBuffer, &BitmapInfo, DIB_RGB_COLORS);


HRESULT CVMR_Capture::SaveJpg(HBITMAP hbmp,CString filename)
{
if(hbmp==NULL)
return -1;
//warning: this code snippet is not bullet proof.
//do error check by yourself [masterz]
PICTDESC picdesc;
picdesc.cbSizeofstruct = sizeof(PICTDESC);
picdesc.picType = PICTYPE_BITMAP ;
picdesc.bmp.hpal = NULL;
picdesc.bmp.hbitmap = hbmp;
IPicture* pPicture=NULL;
OleCreatePictureIndirect(&picdesc, IID_IPicture, TRUE,(VOID**)&pPicture);
//LPSTREAM pStream;

IStream* pStream=NULL;
HRESULT hr;
//JIF(hr=CreateStreamOnHGlobal(NULL,TRUE,&pStream));
JIF(hr=CreateStreamOnHGlobal(NULL, TRUE, (LPSTREAM*)&pStream));

Bitmap bit2(hbmp, NULL);

CLSID pngClsid;
GetEncoderClsid(L"image/jpeg", &pngClsid);

// Setup encoder parameters
EncoderParameters encoderParameters;
encoderParameters.Count = 1;
encoderParameters.Parameter[0].Guid = EncoderQuality;
encoderParameters.Parameter[0].Type = EncoderParameterValueTypeLong;
encoderParameters.Parameter[0].NumberOfValues = 1;

// setup compression level
ULONG quality = 90;
encoderParameters.Parameter[0].Value = &quality;

// Save the image to the stream


Status SaveStatus = bit2.Save(pStream, &pngClsid, &encoderParameters);

if(SaveStatus != Ok)
{
// this shoud free global memory used by the stream
// according to MSDN
pStream->Release();
AfxMessageBox(_T("Failed to save to stream!"));
return -1;
}

// get the size of the stream
ULARGE_INTEGER ulnSize;
LARGE_INTEGER lnOffset;
lnOffset.QuadPart = 0;
if(pStream->Seek(lnOffset, STREAM_SEEK_END, &ulnSize) != S_OK)
{
pStream->Release();
AfxMessageBox(_T("Failed to get the size of the stream!"));
return -1;
}

// now move the pointer to the begining of the file
if(pStream->Seek(lnOffset, STREAM_SEEK_SET, NULL) != S_OK)
{
pStream->Release();
AfxMessageBox(_T("Failed to smove the file pointer to the beginning of the stream!"));
return -1;
}


// I am going to save it to the file just so we can load the jpg to a gfx program
CFile fFile;

if(fFile.Open(_T(filename), CFile::modeCreate | CFile::modeWrite))
{
//ULONGLONG 是 unsigned __int64使用前要进行转化(int)、(unsigned long)
char *pBuff = new char[(int)ulnSize.QuadPart];

// Read the stream directly into the buffer

ULONG ulBytesRead;
if(pStream->Read(pBuff, (unsigned long)ulnSize.QuadPart, &ulBytesRead) != S_OK)
{
pStream->Release();
delete pBuff;
return -1 ;
}

fFile.Write(pBuff, ulBytesRead);
fFile.Close();
delete pBuff;
}
else AfxMessageBox(_T("Failed to save data to the disk!"));

// Free memory used by the stream
pStream->Release();
return 0;

}
booklove 2005-09-30
  • 打赏
  • 举报
回复
http://www.vckbase.net/document/viewdoc/?id=1177
long_xing 2005-09-29
  • 打赏
  • 举报
回复
GDI+中有一个CImage类好象可以转换
寻开心 2005-09-29
  • 打赏
  • 举报
回复
cximage库 www.codeproject.com/bitmap/cximage.asp
intel的jpeg库 http://resource.gameres.com/ijl151.exe
都支持
gunney 2005-09-29
  • 打赏
  • 举报
回复
网上这样的类很多 如果你使用GDI+可能也可以 VS。NET

19,469

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 图形处理/算法
社区管理员
  • 图形处理/算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧