MFC BMP设置Bitmap的分辨率DPI为600点 gdiplus 生成标签
作者:互联网
MFC设置Bitmap 的分辨率DPI (打印机一般是600点)
检索编码器的类标识符 - Win32 apps | Microsoft Docs
- image/bmp
- image/jpeg
- image/gif
- image/tiff
- image/png
using namespace Gdiplus;
int GetEncoderClsid(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;
free(pImageCodecInfo);
return j; // Success
}
}
free(pImageCodecInfo);
return -1; // Failure
}
void setResolution()
{
Gdiplus::Bitmap* pBitmap = new Gdiplus::Bitmap(L"BarCode.bmp");
pBitmap->SetResolution(600, 600);//设置此 Bitmap 的分辨率。
CLSID clsid;
GetEncoderClsid(L"image/bmp", &clsid); //Get the encoder
Gdiplus::Status status=pBitmap->Save(L"BarCode.bmp", &clsid);//Save the image into physical memory
if (status != Gdiplus::Ok) { logger.ERROR_F("保存错误:" + to_string(status)); return; }
}
生成条码标签图片 (在模板上画文字、条码等)
/**********************************************************************
* 函数名:void GenBarCode(HDC hDC, int iL, int iT, int iB, CString csCode, int iStyle, int iMap);
* 参 数:
* hDC:画布句柄
* iL:左上角 X坐标
* iT:左上角 Y坐标
* iB:条码高度
* csCode:条码内容
* iStyle:条码格式
* iMap:
* 返回值:无
* 描 述:画条码到指定HDC
**********************************************************************/
void GenBarCode(HDC hDC, int iL, int iT, int iB, CString csCode, int iStyle, int iMap, int iWidth)
{
COLORREF clrBar = RGB(0, 0, 0);
COLORREF clrSpace = RGB(255, 255, 255);
int iPenW = 2;
switch (iStyle)
{
case 0:
{
Barcode39 code;
code.Encode39(csCode);
code.DrawBarcodeWidth(hDC, iL, iT, iB - 10, iB, clrBar, clrSpace, iPenW, iWidth);
}
break;
case 1:
{
Barcode93 code;
code.Encode93(csCode);
code.DrawBarcodeWidth(hDC, iL, iT, iB - 10, iB, clrBar, clrSpace, iPenW, iWidth);
}
break;
case 2:
{
Barcode128 code;
code.Encode128A(csCode);
code.DrawBarcodeWidth(hDC, iL, iT, iB - 10, iB, clrBar, clrSpace, iPenW, iWidth);
}
break;
case 3:
{
Barcode128 code;
code.Encode128B(csCode);
code.DrawBarcodeWidth(hDC, iL, iT, iB - 10, iB, clrBar, clrSpace, iPenW, iWidth);
}
break;
case 4:
{
Barcode128 code;
code.Encode128C(csCode);
code.DrawBarcodeWidth(hDC, iL, iT, iB - 10, iB, clrBar, clrSpace, iPenW, iWidth);
}
break;
case 5:
{
BarcodeI2of5 code;
code.EncodeI2of5(csCode);
code.DrawBarcodeWidth(hDC, iL, iT, iB - 10, iB, clrBar, clrSpace, iPenW, iWidth);
}
break;
case 6:
{
BarcodeEan13 code;
code.EncodeEan13(csCode);
code.DrawBarcodeWidth(hDC, iL, iT, iB - 10, iB, clrBar, clrSpace, iPenW, iWidth);
}
break;
default:
break;
}
}
//生成条码标签图片 (在模板上画文字、条码等)
void generateDeviceLable(CString g_szTemplateName, CString sn, CString imei1, CString imei2)
{
int height, width;
CRect rect;//定义矩形类
CRect rect1;
CImage image; //创建图片类
//导入模板 自动获取宽高
image.Load(g_szTemplateName);
if (image == NULL) { logger.ERROR_F("模版不存在:"+string(g_szTemplateName)); return; }
//int bpp=image.GetBPP();//32
height = image.GetHeight();
width = image.GetWidth();
HDC memDC = image.GetDC();
RECT rectBmp = { 0,0,width, height };
WORD iStyle = 2; //code128
//630 640
DWORD barCodeSn_left = 71, barCodeSn_top = 440, barCodeSn_bottom = 440 + 102, barCodeSn_UN = 200;
RECT rectTextSN = { 201, barCodeSn_bottom + 7, width - 201, barCodeSn_bottom + 7 + 45 };
DWORD barCodeImei_left = barCodeSn_left, barCodeImei_top = rectTextSN.bottom + 18, barCodeImei_bottom = barCodeImei_top + 102, barCodeImei_UN = 200;
RECT rectTextIMEI = { 201, barCodeImei_bottom + 7, width - 201,barCodeImei_bottom + 7 + 45 };
DWORD barCodeImei_left2 = barCodeSn_left, barCodeImei_top2 = rectTextIMEI.bottom + 18, barCodeImei_bottom2 = barCodeImei_top2 + 102, barCodeImei_UN2 = 200;
RECT rectTextIMEI2 = { 201, barCodeImei_bottom2 + 7, width - 201, barCodeImei_bottom2 + 7 + 45 };
BYTE *bText = (BYTE*)new char[128];
CFont font;
font.CreateFont(54, // nHeight
35, // nWidth
0, // nEscapement
0, // nOrientation
FW_BOLD, // nWeight
FALSE, // bItalic
FALSE, // bUnderline
0, // cStrikeOut
ANSI_CHARSET, // nCharSet
OUT_DEFAULT_PRECIS, // nOutPrecision
CLIP_DEFAULT_PRECIS, // nClipPrecision
DEFAULT_QUALITY, // nQuality
VARIABLE_PITCH | FF_SWISS, // nPitchAndFamily
_T("Arial")); // lpszFac
SelectObject(memDC, font);
SetBkMode(memDC, TRANSPARENT);
//画条码
GenBarCode(memDC, barCodeSn_left, barCodeSn_top, barCodeSn_bottom, sn, iStyle, 0, width - barCodeSn_left * 2);
//写号
memset(bText, 0x00, sizeof(bText));
sprintf((char *)bText, "SN:%s", sn);
DrawText(memDC, (LPCSTR)bText, -1, &rectTextSN, DT_VCENTER);
//画条码
GenBarCode(memDC, barCodeImei_left, barCodeImei_top, barCodeImei_bottom, imei1, iStyle, 0, width - barCodeSn_left * 2);
//写号
memset(bText, 0x00, sizeof(bText));
sprintf((char *)bText, "IMEI1:%s", imei1);
DrawText(memDC, (LPCSTR)bText, -1, &rectTextIMEI, DT_VCENTER);
//画条码
GenBarCode(memDC, barCodeImei_left2, barCodeImei_top2, barCodeImei_bottom2, imei2, iStyle, 0, width - barCodeSn_left * 2);
//写号
memset(bText, 0x00, sizeof(bText));
sprintf((char *)bText, "IMEI2:%s", imei2);
DrawText(memDC, (LPCSTR)bText, -1, &rectTextIMEI2, DT_VCENTER);
image.ReleaseDC();
DeleteObject(font);
delete(bText);
system("del BarCode.bmp"); //先删除
image.Save("BarCode.bmp");
image.Destroy();
Gdiplus::Bitmap* pBitmap = new Gdiplus::Bitmap(L"BarCode.bmp");
pBitmap->SetResolution(600, 600);//设置此 Bitmap 的分辨率。
CLSID jpgClsid;
GetEncoderClsid(L"image/bmp", &jpgClsid); //Get the encoder
Gdiplus::Status status=pBitmap->Save(L"BarCode.bmp", &jpgClsid);//Save the jpg image into physical memory
if (status != Gdiplus::Ok) { logger.ERROR_F("保存错误:" + to_string(status)); return; }
}
标签:code,600,int,image,Bitmap,bText,MFC,iB,barCodeImei 来源: https://blog.csdn.net/chenhao0568/article/details/122858575