当前位置:网站首页>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; }
}边栏推荐
- The 2022 China low / no code Market Research and model selection evaluation report was released
- Add salt and pepper noise or Gaussian noise to the picture
- An example of multi module collaboration based on NCF
- [shell] clean up nohup Out file
- What are the common message queues?
- pytorch_ 01 automatic derivation mechanism
- sql查询:将下一行减去上一行,并做相应的计算
- cf:C. Column Swapping【排序 + 模拟】
- SAP ABAP BDC(批量数据通信)-018
- 980. Different path III DFS
猜你喜欢

Loss function and positive and negative sample allocation in target detection: retinanet and focal loss

PowerPivot——DAX(函数)

Flink SQL realizes reading and writing redis and dynamically generates hset key

一个简单的代数问题的求解

Why does the data center need a set of infrastructure visual management system

如何提高网站权重

驱动开发中platform设备驱动架构详解

架构设计的五个核心要素

C nullable type

Harmonyos practice - Introduction to development, analysis of atomized services
随机推荐
盘点国内有哪些EDA公司?
Things about data storage 2
What is dependency injection (DI)
Différenciation et introduction des services groupés, distribués et microservices
sql查询:将下一行减去上一行,并做相应的计算
集群、分布式、微服务的区别和介绍
PTA TIANTI game exercise set l2-003 moon cake test point 2, test point 3 Analysis
"Multimodal" concept
SAP ABAP BDC (batch data communication) -018
PTA 天梯赛练习题集 L2-004 搜索树判断
Nodejs get client IP
980. Different path III DFS
TCC of distributed transaction solutions
牙齿干细胞的存储问题(未完待续)
What EDA companies are there in China?
目标检测中的损失函数与正负样本分配:RetinaNet与Focal loss
高级程序员必知必会,一文详解MySQL主从同步原理,推荐收藏
上海字节面试问题及薪资福利
【已解决】记一次EasyExcel的报错【读取xls文件时全表读不报错,指定sheet名读取报错】
Data storage 3