当前位置:网站首页>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; }
}
边栏推荐
- Go语学习笔记 - gorm使用 - 原生sql、命名参数、Rows、ToSQL | Web框架Gin(九)
- Realize GDB remote debugging function between different network segments
- Add salt and pepper noise or Gaussian noise to the picture
- 线性回归
- 关于服装ERP,你知道多少?
- AI face editor makes Lena smile
- 上海字节面试问题及薪资福利
- mac版php装xdebug环境(m1版)
- Message queue: how to deal with message backlog?
- zabbix_ Get test database failed
猜你喜欢
What is message queuing?
Différenciation et introduction des services groupés, distribués et microservices
Loss function and positive and negative sample allocation in target detection: retinanet and focal loss
Web authentication API compatible version information
C#可空类型
Go语学习笔记 - gorm使用 - 原生sql、命名参数、Rows、ToSQL | Web框架Gin(九)
EMMC print cqhci: timeout for tag 10 prompt analysis and solution
分布式全局ID生成方案
搞懂fastjson 对泛型的反序列化原理
sql查询:将下一行减去上一行,并做相应的计算
随机推荐
三级菜单数据实现,实现嵌套三级菜单数据
PTA 天梯赛练习题集 L2-002 链表去重
往图片添加椒盐噪声或高斯噪声
线性回归
Bbox regression loss function in target detection -l2, smooth L1, IOU, giou, Diou, ciou, focal eiou, alpha IOU, Siou
404 not found service cannot be reached in SAP WebService test
STM32 key state machine 2 - state simplification and long press function addition
Unity keeps the camera behind and above the player
Bat instruction processing details
ForkJoin最全详解(从原理设计到使用图解)
Realize GDB remote debugging function between different network segments
Three level menu data implementation, nested three-level menu data
PowerPivot - DAX (function)
zabbix_get测试数据库失败
老板总问我进展,是不信任我吗?(你觉得呢)
【SQL实战】一条SQL统计全国各地疫情分布情况
集群、分布式、微服務的區別和介紹
Things about data storage 2
【日常训练--腾讯精选50】292. Nim 游戏
牙齿干细胞的存储问题(未完待续)