请问怎么样动态的显示JPG格式的图片?

xqk 2003-09-11 10:12:27
已经有一个控件PICTURE

HBITMAP hbmp;
hbmp = (HBITMAP)LoadImage(AfxGetInstanceHandle(),"C:\\Winnt\\kp2003_1M.bmp"/*"D:\\csoft\\NTOOLS\\00\\04\\HTML\\JPG\\001.jpg"*/,IMAGE_BITMAP,400,300,LR_LOADFROMFILE | LR_CREATEDIBSECTION);

m_Pic1.SetBitmap(hbmp);

但是我这样做显示.bmp没有问题,但是却无法显示.jpg格式的文件,请问大家谁知道如何
显示.jpg格式的文件?
...全文
384 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
abc_rain 2003-09-12
  • 打赏
  • 举报
回复
你可以在Load和Draw函数里稍改一下就可以如下:
CRect rcTemp;
m_Picture[ID].SubclassDlgItem(145+ID,this);
m_Picture[ID].GetClientRect(rcTemp);
m_Picture[ID].Load(strFileNmae,rcTemp.Width(),rcTemp.Height());
m_Picture[ID].Draw(rcTemp.Width(),rcTemp.Height());
xqk 2003-09-12
  • 打赏
  • 举报
回复
help me
bluebohe 2003-09-12
  • 打赏
  • 举报
回复
从网上下载一个库吧,这个库做图像处理不错
http://www.vchelp.net/vchelp/file2002_4/flib.asp?type_id=18&class_id=1&cata_id=12&article_id=907
abc_rain 2003-09-12
  • 打赏
  • 举报
回复
BOOL CPictureEx::Load(LPCTSTR szFileName,int cx,int cy)
{
strFileName.Format("%s",szFileName);
CFile file;
HGLOBAL hGlobal;
DWORD dwSize;

if (!file.Open(szFileName,
CFile::modeRead |
CFile::shareDenyWrite) )
{
TRACE(_T("Load (file): Error opening file %s\n"),szFileName);
return FALSE;
};

dwSize = file.GetLength();
hGlobal = GlobalAlloc(GMEM_MOVEABLE | GMEM_NODISCARD,dwSize);
if (!hGlobal)
{
TRACE(_T("Load (file): Error allocating memory\n"));
return FALSE;
};

char *pData = reinterpret_cast<char*>(GlobalLock(hGlobal));
if (!pData)
{
TRACE(_T("Load (file): Error locking memory\n"));
GlobalFree(hGlobal);
return FALSE;
};

TRY
{
file.Read(pData,dwSize);
}
CATCH(CFileException, e);
{
TRACE(_T("Load (file): An exception occured while reading the file %s\n"),
szFileName);
GlobalFree(hGlobal);
e->Delete();
file.Close();
return FALSE;
}
END_CATCH
GlobalUnlock(hGlobal);
file.Close();

BOOL bRetValue = Load(hGlobal,dwSize,cx,cy);
GlobalFree(hGlobal);
return bRetValue;
}

BOOL CPictureEx::Draw(int cx,int cy)
{
if (!m_bIsInitialized)
{
TRACE(_T("Call one of the CPictureEx::Load() member functions before calling Draw()\n"));
return FALSE;
};

if (IsAnimatedGIF())
{
// the picture needs animation
// we'll start the thread that will handle it for us

unsigned int nDummy;
m_hThread = (HANDLE) _beginthreadex(NULL,0,_ThreadAnimation,this,
CREATE_SUSPENDED,&nDummy);
if (!m_hThread)
{
TRACE(_T("Draw: Couldn't start a GIF animation thread\n"));
return FALSE;
}
else
ResumeThread(m_hThread);
}
else
{
if (m_pPicture)
{
long hmWidth;
long hmHeight;
m_pPicture->get_Width(&hmWidth);
m_pPicture->get_Height(&hmHeight);
if(cx==0)
{
if (m_pPicture->Render(m_hMemDC, 0, 0, m_PictureSize.cx , m_PictureSize.cy, 0, hmHeight, hmWidth, -hmHeight, NULL) == S_OK)
{
Invalidate(FALSE);
return TRUE;
};
}
if (m_pPicture->Render(m_hMemDC, 0, 0, cx, cy, 0, hmHeight, hmWidth, -hmHeight, NULL) == S_OK)
{
Invalidate(FALSE);
return TRUE;
};
};
};

return FALSE;
}


BOOL CPictureEx::Load(HGLOBAL hGlobal, DWORD dwSize,int cx,int cy)
{
IStream *pStream = NULL;
UnLoad();

if (!(m_pRawData = reinterpret_cast<unsigned char*> (GlobalLock(hGlobal))) )
{
TRACE(_T("Load: Error locking memory\n"));
return FALSE;
};

m_nDataSize = dwSize;
m_pGIFHeader = reinterpret_cast<TGIFHeader *> (m_pRawData);

if ((memcmp(&m_pGIFHeader->m_cSignature,"GIF",3) != 0) &&
((memcmp(&m_pGIFHeader->m_cVersion,"87a",3) != 0) ||
(memcmp(&m_pGIFHeader->m_cVersion,"89a",3) != 0)) )
{
// it's neither GIF87a nor GIF89a
// do the default processing

// clear GIF variables
m_pRawData = NULL;
GlobalUnlock(hGlobal);

// don't delete memory on object's release
if (CreateStreamOnHGlobal(hGlobal,FALSE,&pStream) != S_OK)
return FALSE;

if (OleLoadPicture(pStream,dwSize,FALSE,IID_IPicture,
reinterpret_cast<LPVOID *>(&m_pPicture)) != S_OK)
{
pStream->Release();
return FALSE;
};
pStream->Release();

// store picture's size

long hmWidth;
long hmHeight;
m_pPicture->get_Width(&hmWidth);
m_pPicture->get_Height(&hmHeight);

HDC hDC = ::GetDC(m_hWnd);
m_PictureSize.cx = MulDiv(hmWidth, GetDeviceCaps(hDC,LOGPIXELSX), 2540);
m_PictureSize.cy = MulDiv(hmHeight, GetDeviceCaps(hDC,LOGPIXELSY), 2540);
::ReleaseDC(m_hWnd,hDC);
}
else
{
// it's a GIF
m_bIsGIF = TRUE;
m_pGIFLSDescriptor = reinterpret_cast<TGIFLSDescriptor *>
(m_pRawData + sizeof(TGIFHeader));
if (m_pGIFLSDescriptor->GetPackedValue(LSD_PACKED_GLOBALCT) == 1)
{
// calculate the globat color table size
m_nGlobalCTSize = static_cast<int>
(3* (1 << (m_pGIFLSDescriptor->GetPackedValue(LSD_PACKED_GLOBALCTSIZE)+1)));
// get the background color if GCT is present
unsigned char *pBkClr = m_pRawData + sizeof(TGIFHeader) +
sizeof(TGIFLSDescriptor) + 3*m_pGIFLSDescriptor->m_cBkIndex;
m_clrBackground = RGB(pBkClr[0],pBkClr[1],pBkClr[2]);
};

// store the picture's size
m_PictureSize.cx = m_pGIFLSDescriptor->m_wWidth;
m_PictureSize.cy = m_pGIFLSDescriptor->m_wHeight;

// determine frame count for this picture
UINT nFrameCount=0;
ResetDataPointer();
while (SkipNextGraphicBlock())
nFrameCount++;

#ifdef GIF_TRACING
TRACE(
_T(" -= GIF encountered\n"
"Logical Screen dimensions = %dx%d\n"
"Global color table = %d\n"
"Color depth = %d\n"
"Sort flag = %d\n"
"Size of Global Color Table = %d\n"
"Background color index = %d\n"
"Pixel aspect ratio = %d\n"
"Frame count = %d\n"
"Background color = %06Xh\n\n"
),
m_pGIFLSDescriptor->m_wWidth,
m_pGIFLSDescriptor->m_wHeight,
m_pGIFLSDescriptor->GetPackedValue(LSD_PACKED_GLOBALCT),
m_pGIFLSDescriptor->GetPackedValue(LSD_PACKED_CRESOLUTION),
m_pGIFLSDescriptor->GetPackedValue(LSD_PACKED_SORT),
m_pGIFLSDescriptor->GetPackedValue(LSD_PACKED_GLOBALCTSIZE),
m_pGIFLSDescriptor->m_cBkIndex,
m_pGIFLSDescriptor->m_cPixelAspect,
nFrameCount,
m_clrBackground
);
EnumGIFBlocks();
#endif

if (nFrameCount == 0) // it's an empty GIF!
{
m_pRawData = NULL;
GlobalUnlock(hGlobal);
return FALSE;
};

// now check the frame count
// if there's only one frame, no need to animate this GIF
// therefore, treat it like any other pic

if (nFrameCount == 1)
{
// clear GIF variables
m_pRawData = NULL;
GlobalUnlock(hGlobal);

// don't delete memory on object's release
if (CreateStreamOnHGlobal(hGlobal,FALSE,&pStream) != S_OK)
return FALSE;

if (OleLoadPicture(pStream,dwSize,FALSE,IID_IPicture,
(LPVOID *)&m_pPicture) != S_OK)
{
pStream->Release();
return FALSE;
};

pStream->Release();
}
else
{
// if, on the contrary, there are several frames
// then store separate frames in an array

TFrame frame;
UINT nBlockLen;
HGLOBAL hFrameData;
UINT nCurFrame = 0;

ResetDataPointer();
while (hFrameData = GetNextGraphicBlock(&nBlockLen,
&frame.m_nDelay, &frame.m_frameSize,
&frame.m_frameOffset, &frame.m_nDisposal) )
{
#ifdef GIF_TRACING
//////////////////////////////////////////////
// uncomment the following strings if you want
// to write separate frames on disk
//
// CString szName;
// szName.Format(_T("%.4d.gif"),nCurFrame);
// WriteDataOnDisk(szName,hFrameData,nBlockLen);
// nCurFrame++;
#endif // GIF_TRACING

IStream *pStream = NULL;

// delete memory on object's release
if (CreateStreamOnHGlobal(hFrameData,TRUE,&pStream) != S_OK)
{
GlobalFree(hFrameData);
continue;
};

if (OleLoadPicture(pStream,nBlockLen,FALSE,
IID_IPicture,
reinterpret_cast<LPVOID *>(&frame.m_pPicture)) != S_OK)
{
pStream->Release();
continue;
};
pStream->Release();

// everything went well, add this frame
m_arrFrames.push_back(frame);
};

// clean after ourselves
m_pRawData = NULL;
GlobalUnlock(hGlobal);

if (m_arrFrames.empty()) // couldn't load any frames
return FALSE;
};
}; // if (!IsGIF...
if(cx==0)
{
return PrepareDC(m_PictureSize.cx ,m_PictureSize.cy);
}
return PrepareDC(cx,cy);
// return TRUE;
}
哈密尔 2003-09-11
  • 打赏
  • 举报
回复
HBITMAP,顾名思义,当然是用来bitmap格式的文件,jpg格式的文件不一样是压缩过的
abc_rain 2003-09-11
  • 打赏
  • 举报
回复
用CPictureEx,我就用它作的动态的显示JPG格式的图片效果很好
http://www.codeproject.com/bitmap/pictureex.asp?target=cpictureex
flyycyu 2003-09-11
  • 打赏
  • 举报
回复
jpg和bmp格式当然不同了
你可以看jpeg/mpeg编解码一书!
xqk 2003-09-11
  • 打赏
  • 举报
回复
abc_rain() ,

请问这个CPictureEx类里边有没有 可以调整图片大小的函数啊?

16,471

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

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