如何将控件内的显示内容保存成JPG图片???

hfh01 2003-04-05 09:29:46
我自己做了一个ACTIVEX控件,功能是用户可以在上面画线、圆,我现在想将控件内的内容保存成JPG图片,该怎么做?
...全文
105 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
用户 昵称 2003-04-05
  • 打赏
  • 举报
回复
GDI+ support tif/jpg/gif
#include <Stdio.h>
#include <Objbase.h>
#include <Windows.h>
#include <Gdiplus.h>
#include <GdiPlusEnums.h>
using namespace Gdiplus;
#pragma comment(lib,"gdiplus")
int GetCodecClsid(const WCHAR*, CLSID*);
HRESULT __fastcall AnsiToUnicode(LPCSTR pszA, LPOLESTR* ppszW)
{
ULONG cCharacters;
DWORD dwError;
if (NULL == pszA)
{
*ppszW = NULL;
return NOERROR;
}
cCharacters = strlen(pszA)+1;
*ppszW = (LPOLESTR) CoTaskMemAlloc(cCharacters*2);
if (NULL == *ppszW)
return E_OUTOFMEMORY;
if (0 == MultiByteToWideChar(CP_ACP, 0, pszA, cCharacters,*ppszW, cCharacters))
{
dwError = GetLastError();
CoTaskMemFree(*ppszW);
*ppszW = NULL;
return HRESULT_FROM_WIN32(dwError);
}
return NOERROR;
}

int main(int argc, char* argv[])
{
int n_ret=0;
printf("%s can convert jpg, png, gif, tiff to gif file.\n if it works, it is written by masterz, otherwise I don't know who write it\n",argv[0]);
if(argc!=3)
{
printf("usage:%s gif_filename source_image_filename\n",argv[0]);
return -1;
}
GdiplusStartupInput gdiplusStartupInput;
ULONG gdiplusToken;
GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
{
CLSID codecClsid;
Status stat;
EncoderParameters encoderParameters;

GetCodecClsid(L"image/gif", &codecClsid);
encoderParameters.Count = 0;
LPOLESTR bstr_src_img;
LPOLESTR bstr_dst_fn;
AnsiToUnicode(argv[1],&bstr_dst_fn);
AnsiToUnicode(argv[2],&bstr_src_img);
Image image(bstr_src_img);
stat =image.Save(bstr_dst_fn, &codecClsid, &encoderParameters);
if(stat == Ok)
{
n_ret=0;
wprintf(L"%s saved successfully.\n",bstr_dst_fn);
}
else
{
n_ret=-2;
wprintf(L"%d Attempt to save %s failed.\n", stat, bstr_dst_fn);
}
CoTaskMemFree(bstr_dst_fn);
CoTaskMemFree(bstr_src_img);
}
GdiplusShutdown(gdiplusToken);
return 0;
} // main
int GetCodecClsid(const WCHAR* format, CLSID* pClsid)
{
UINT num = 0; // number of image encoders
UINT size = 0; // size of the image encoder array in bytes

ImageCodecInfo* pImageCodecInfo = NULL;

GetImageEncodersSize(&num, &size);
if(size == 0)
return -1; // Failure

pImageCodecInfo = (ImageCodecInfo*)(malloc(size));
if(pImageCodecInfo == NULL)
return -1; // Failure

GetImageEncoders(num, size, pImageCodecInfo);

for(UINT j = 0; j < num; ++j)
{
if( wcscmp(pImageCodecInfo[j].MimeType, format) == 0 )
{
*pClsid = pImageCodecInfo[j].Clsid;
return j; // Success
}
} // for

return -1; // Failure

} // GetCodecClsid
//in order to compile this program, you need to download microsoft platform sdk from http://www.microsoft.com/msdownload/platformsdk/sdkupdate/downlevel.htm
//also download gdiplus.dll and put it at the same directory as the executable.
//http://www.microsoft.com/downloads/release.asp?releaseid=32738
//Platform SDK Redistributable: GDI+ RTM

//sample usage: img2gif a.gif src.bmp
用户 昵称 2003-04-05
  • 打赏
  • 举报
回复
保存一个HBITMAP或者CBitmap到指定文件,参考下面的代码:
=========================================================

int GetDIBPixelSize(const BITMAPINFOHEADER & bmih)
{
if ( bmih.biSizeImage )
return bmih.biSizeImage;
else
return ( bmih.biWidth * bmih.biBitCount + 31 ) / 32 * 4 * bmih.biPlanes * abs(bmih.biHeight);
}

int GetDIBColorCount(const BITMAPINFOHEADER & bmih)
{
if ( bmih.biBitCount <= 8 )
if ( bmih.biClrUsed )
return bmih.biClrUsed;
else
return 1 << bmih.biBitCount;
else if ( bmih.biCompression==BI_BITFIELDS )
return 3 + bmih.biClrUsed;
else
return bmih.biClrUsed;
}

BOOL SaveDIBToBmp(const char* pFileName, const BITMAPINFO *pBMI, const BYTE *pBits)
{
if(pFileName==NULL){
return FALSE;
}

HANDLE handle = CreateFile(pFileName, GENERIC_WRITE, FILE_SHARE_READ,
NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);

if(handle == INVALID_HANDLE_VALUE){
return FALSE;
}

BITMAPFILEHEADER bmFH;

int nHeadSize = sizeof(BITMAPINFOHEADER) +
sizeof(RGBQUAD) * GetDIBColorCount(pBMI->bmiHeader);

bmFH.bfType = 0x4D42;
bmFH.bfSize = nHeadSize + GetDIBPixelSize(pBMI->bmiHeader);
bmFH.bfReserved1 = 0;
bmFH.bfReserved2 = 0;
bmFH.bfOffBits = nHeadSize + sizeof(BITMAPFILEHEADER);

DWORD dwRead = 0;
WriteFile(handle, & bmFH, sizeof(bmFH), & dwRead, NULL);

if(pBits==NULL) // packed DIB
pBits = (BYTE *) pBMI + nHeadSize;

WriteFile(handle, pBMI, nHeadSize, & dwRead, NULL);
WriteFile(handle, pBits, GetDIBPixelSize(pBMI->bmiHeader), & dwRead, NULL);

CloseHandle(handle);

return TRUE;
}

BOOL CImageBmp::SaveFile(const char * pFileName)
{
return SaveAsBmp(pFileName);
}

BITMAPINFO * BitmapToDIB(HPALETTE hPal, // palette for color conversion
HBITMAP hBmp, // DDB for convert
int nBitCount, int nCompression) // format wanted
{
typedef struct
{
BITMAPINFOHEADER bmiHeader;
RGBQUAD bmiColors[256+3];
} DIBINFO;

BITMAP ddbinfo;
DIBINFO dibinfo;

// retrieve DDB information
if ( GetObject(hBmp, sizeof(BITMAP), & ddbinfo)==0 )
return NULL;

// fill out BITMAPINFOHEADER based on size and required format
memset(&dibinfo, 0, sizeof(dibinfo));

dibinfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
dibinfo.bmiHeader.biWidth = ddbinfo.bmWidth;
dibinfo.bmiHeader.biHeight = ddbinfo.bmHeight;
dibinfo.bmiHeader.biPlanes = 1;
dibinfo.bmiHeader.biBitCount = nBitCount;
dibinfo.bmiHeader.biCompression = nCompression;

HDC hDC = GetDC(NULL); // screen DC
HGDIOBJ hpalOld;

if ( hPal )
hpalOld = SelectPalette(hDC, hPal, FALSE);
else
hpalOld = NULL;

// query GDI for image size
GetDIBits(hDC, hBmp, 0, ddbinfo.bmHeight, NULL, (BITMAPINFO *) & dibinfo, DIB_RGB_COLORS);

int nInfoSize = sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * GetDIBColorCount(dibinfo.bmiHeader);
int nTotalSize = nInfoSize + GetDIBPixelSize(dibinfo.bmiHeader);

BYTE * pDIB = new BYTE[nTotalSize];

if ( pDIB )
{
memcpy(pDIB, & dibinfo, nInfoSize);

if ( ddbinfo.bmHeight != GetDIBits(hDC, hBmp, 0, ddbinfo.bmHeight, pDIB + nInfoSize, (BITMAPINFO *) pDIB, DIB_RGB_COLORS) )
{
delete [] pDIB;
pDIB = NULL;
}
}

if ( hpalOld )
SelectObject(hDC, hpalOld);

ReleaseDC(NULL, hDC);

return (BITMAPINFO *) pDIB;
}


HBITMAP CaptureWindow(HWND hWnd)
{
RECT wnd;

if ( ! GetWindowRect(hWnd, & wnd) )
return NULL;

HDC hDC = GetWindowDC(hWnd);

HBITMAP hBmp = CreateCompatibleBitmap(hDC, wnd.right - wnd.left, wnd.bottom - wnd.top);

if ( hBmp )
{
HDC hMemDC = CreateCompatibleDC(hDC);
HGDIOBJ hOld = SelectObject(hMemDC, hBmp);

BitBlt(hMemDC, 0, 0, wnd.right - wnd.left, wnd.bottom - wnd.top,
hDC, 0, 0, SRCCOPY);

SelectObject(hMemDC, hOld);
DeleteObject(hMemDC);
}

ReleaseDC(hWnd, hDC);

return hBmp;
}

void SaveWindowToFile(HWND hWnd, const char *filename, int nBitCount, int nCompression)
{
HBITMAP hBmp = CaptureWindow(hWnd);

if(hBmp)
{
BITMAPINFO * pDIB = BitmapToDIB(NULL, hBmp, nBitCount, nCompression);
if(pDIB)
{
SaveDIBToBmp(filename, pDIB, NULL);
delete [](BYTE *) pDIB;
}
DeleteObject(hBmp);
}
}
huangbeyond 2003-04-05
  • 打赏
  • 举报
回复
1.把ActiveX里显示的位图保存为BMP;
2.把BMP保存为JPG(可以使用很多提供全部代码的类库);

15,979

社区成员

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

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