当前位置:网站首页>MFC BMP sets the resolution of bitmap, DPI is 600 points, and gdiplus generates labels
MFC BMP sets the resolution of bitmap, DPI is 600 points, and gdiplus generates labels
2022-07-07 05:55:00 【Little yellow man software】
MFC Set up Bitmap The resolution of the DPI ( Printers are generally 600 spot )
Retrieve the class identifier of the encoder - Win32 apps | Microsoft Docs
- image/bmp
- image/jpeg
- image/gif
- image/tiff
- image/png
#include <atlbase.h>
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
}
BOOL checkBMPResolution(LPCTSTR fileName, float dpi)
{
// Initialize GDI+.
GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR gdiplusToken;
GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
USES_CONVERSION;
Gdiplus::Bitmap* pBitmap = new Gdiplus::Bitmap(T2W(fileName));
if (pBitmap == NULL || pBitmap->GetLastStatus() != Gdiplus::Ok) { logger.ERROR_F(__FUNCTION__+to_string(__LINE__)+" error :" + to_string(pBitmap->GetLastStatus())); delete pBitmap; return FALSE; }
if (pBitmap->GetHorizontalResolution() != dpi || pBitmap->GetVerticalResolution() != dpi)
{
logger.ERROR_F(" Horizontal resolution " + to_string(pBitmap->GetHorizontalResolution()) + " Or vertical resolution " + to_string(pBitmap->GetVerticalResolution()) + " error , Should be " + to_string(dpi));
delete pBitmap;
return FALSE;
}
delete pBitmap;
return TRUE;
}
BOOL setBMPResolution(LPCTSTR fileName, float dpi)
{
LPCTSTR outFileName = "temp.bmp";
// Initialize GDI+.
GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR gdiplusToken;
GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
USES_CONVERSION;
Gdiplus::Bitmap* pBitmap = new Gdiplus::Bitmap(T2W(fileName));
if (pBitmap==NULL || pBitmap->GetLastStatus() != Gdiplus::Ok) { logger.ERROR_F(" Error in resolution conversion :" + to_string(pBitmap->GetLastStatus())); delete pBitmap; return FALSE; }
Gdiplus::Status status = pBitmap->SetResolution(dpi, dpi);// Set this Bitmap The resolution of the .
if (status != Gdiplus::Ok) { logger.ERROR_F(" Turn resolution :" + to_string(status)); delete pBitmap; return FALSE; }
CLSID clsid;
GetEncoderClsid(L"image/bmp", &clsid); //Get the encoder
status = pBitmap->Save(T2W(outFileName), &clsid);//Save the image into physical memory
if (status != Gdiplus::Ok) { logger.ERROR_F(" After resolution conversion , Save error :" + to_string(status)); delete pBitmap; return FALSE; }
delete pBitmap;
pipe_system("del "+ (CString)fileName+"\n"); //cmd Delete first
pipe_system("rename " + (CString)outFileName + " "+ fileName +"\n"); //cmd Rename to the original name
return TRUE;
}
void PrintBitmap(LPCTSTR filename)
{
setBMPResolution(filename, 600);// To 600 The resolution of the
checkBMPResolution(filename,600); // Check for correctness
}
Generate barcode label pictures ( Draw text on the template 、 Bar code, etc )
/**********************************************************************
* Function name :void GenBarCode(HDC hDC, int iL, int iT, int iB, CString csCode, int iStyle, int iMap);
* ginseng Count :
* hDC: Canvas handle
* iL: top left corner X coordinate
* iT: top left corner Y coordinate
* iB: Bar code height
* csCode: Bar code content
* iStyle: Bar code format
* iMap:
* Return value : nothing
* Sketch Statement : Draw the barcode to the specified 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;
}
}
// Generate barcode label pictures ( Draw text on the template 、 Bar code, etc )
void generateDeviceLable(CString g_szTemplateName, CString sn, CString imei1, CString imei2)
{
int height, width;
CRect rect;// Define rectangle class
CRect rect1;
CImage image; // Create picture class
// Import template Automatically obtain width and height
image.Load(g_szTemplateName);
if (image == NULL) { logger.ERROR_F(" Template does not exist :"+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);
// Draw bar code
GenBarCode(memDC, barCodeSn_left, barCodeSn_top, barCodeSn_bottom, sn, iStyle, 0, width - barCodeSn_left * 2);
// Write number
memset(bText, 0x00, sizeof(bText));
sprintf((char *)bText, "SN:%s", sn);
DrawText(memDC, (LPCSTR)bText, -1, &rectTextSN, DT_VCENTER);
// Draw bar code
GenBarCode(memDC, barCodeImei_left, barCodeImei_top, barCodeImei_bottom, imei1, iStyle, 0, width - barCodeSn_left * 2);
// Write number
memset(bText, 0x00, sizeof(bText));
sprintf((char *)bText, "IMEI1:%s", imei1);
DrawText(memDC, (LPCSTR)bText, -1, &rectTextIMEI, DT_VCENTER);
// Draw bar code
GenBarCode(memDC, barCodeImei_left2, barCodeImei_top2, barCodeImei_bottom2, imei2, iStyle, 0, width - barCodeSn_left * 2);
// Write number
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"); // Delete first
image.Save("BarCode.bmp"); // The default resolution is 96
image.Destroy();
Gdiplus::Bitmap* pBitmap = new Gdiplus::Bitmap(L"BarCode.bmp");
pBitmap->SetResolution(600, 600);// Set this Bitmap The resolution of the .
CLSID jpgClsid;
GetEncoderClsid(L"image/bmp", &jpgClsid); //Get the encoder
Gdiplus::Status status=pBitmap->Save(L"BarCode600.bmp", &jpgClsid);//Save the jpg image into physical memory
if (status != Gdiplus::Ok) { logger.ERROR_F(" Save error :" + to_string(status)); return; }
}
边栏推荐
- Flinksql read / write PgSQL
- 得物客服一站式工作台卡顿优化之路
- [daily training -- Tencent selected 50] 235 Nearest common ancestor of binary search tree
- 常用消息队列有哪些?
- On the difference between FPGA and ASIC
- Reptile exercises (III)
- sql查询:将下一行减去上一行,并做相应的计算
- 目标检测中的BBox 回归损失函数-L2,smooth L1,IoU,GIoU,DIoU,CIoU,Focal-EIoU,Alpha-IoU,SIoU
- 数字IC面试总结(大厂面试经验分享)
- Input of native applet switches between text and password types
猜你喜欢
What is dependency injection (DI)
ForkJoin最全详解(从原理设计到使用图解)
SAP ABAP BDC(批量数据通信)-018
如果不知道这4种缓存模式,敢说懂缓存吗?
常用消息队列有哪些?
Hcip eighth operation
TCC of distributed transaction solutions
Go语学习笔记 - gorm使用 - 原生sql、命名参数、Rows、ToSQL | Web框架Gin(九)
Mac version PHP installed Xdebug environment (M1 version)
An example of multi module collaboration based on NCF
随机推荐
【Shell】清理nohup.out文件
ForkJoin最全详解(从原理设计到使用图解)
老板总问我进展,是不信任我吗?(你觉得呢)
Distributed global ID generation scheme
Go语学习笔记 - gorm使用 - gorm处理错误 | Web框架Gin(十)
毕业之后才知道的——知网查重原理以及降重举例
Win configuration PM2 boot auto start node project
Differences and introduction of cluster, distributed and microservice
PTA TIANTI game exercise set l2-003 moon cake test point 2, test point 3 Analysis
What is message queuing?
原生小程序 之 input切換 text與password類型
得物客服一站式工作台卡顿优化之路
Web Authentication API兼容版本信息
Wechat applet Bluetooth connects hardware devices and communicates. Applet Bluetooth automatically reconnects due to abnormal distance. JS realizes CRC check bit
Reading the paper [sensor enlarged egocentric video captioning with dynamic modal attention]
Mybaits multi table query (joint query, nested query)
What is make makefile cmake qmake and what is the difference?
目标检测中的损失函数与正负样本分配:RetinaNet与Focal loss
Harmonyos practice - Introduction to development, analysis of atomized services
SQL query: subtract the previous row from the next row and make corresponding calculations