当前位置:网站首页>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; }
}边栏推荐
- Ten stages of becoming a Senior IC Design Engineer. What stage are you in now?
- Go语学习笔记 - gorm使用 - 原生sql、命名参数、Rows、ToSQL | Web框架Gin(九)
- Reading notes of Clickhouse principle analysis and Application Practice (6)
- win配置pm2开机自启node项目
- Question 102: sequence traversal of binary tree
- 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
- Polynomial locus of order 5
- Hcip eighth operation
- 【已解决】记一次EasyExcel的报错【读取xls文件时全表读不报错,指定sheet名读取报错】
猜你喜欢

SQL query: subtract the previous row from the next row and make corresponding calculations

搞懂fastjson 对泛型的反序列化原理

《2022中国低/无代码市场研究及选型评估报告》发布

Three level menu data implementation, nested three-level menu data

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

往图片添加椒盐噪声或高斯噪声

产业金融3.0:“疏通血管”的金融科技

判断文件是否为DICOM文件

Realize GDB remote debugging function between different network segments

随机生成session_id
随机推荐
OpenSergo 即将发布 v1alpha1,丰富全链路异构架构的服务治理能力
Polynomial locus of order 5
Go 語言的 Context 詳解
Go language context explanation
[cloud native] what is the microservice architecture?
linear regression
《ClickHouse原理解析与应用实践》读书笔记(6)
Reading the paper [sensor enlarged egocentric video captioning with dynamic modal attention]
PTA ladder game exercise set l2-002 linked list de duplication
What is message queuing?
PowerPivot - DAX (function)
C#可空类型
On the difference between FPGA and ASIC
数字IC面试总结(大厂面试经验分享)
TCC of distributed transaction solutions
Determine whether the file is a DICOM file
Flinksql read / write PgSQL
Web architecture design process
Nvisual network visualization
Différenciation et introduction des services groupés, distribués et microservices