当前位置:网站首页>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; }
}
边栏推荐
猜你喜欢
C#可空类型
Message queuing: how to ensure that messages are not lost
C nullable type
【日常训练--腾讯精选50】235. 二叉搜索树的最近公共祖先
Différenciation et introduction des services groupés, distribués et microservices
力扣102题:二叉树的层序遍历
An example of multi module collaboration based on NCF
JVM the truth you need to know
三级菜单数据实现,实现嵌套三级菜单数据
Web architecture design process
随机推荐
线性回归
Pytorch builds neural network to predict temperature
R language [logic control] [mathematical operation]
Web authentication API compatible version information
make makefile cmake qmake都是什么,有什么区别?
STM32按键状态机2——状态简化与增加长按功能
Hcip seventh operation
Bbox regression loss function in target detection -l2, smooth L1, IOU, giou, Diou, ciou, focal eiou, alpha IOU, Siou
Flask1.1.4 Werkzeug1.0.1 源碼分析:啟動流程
关于服装ERP,你知道多少?
PowerPivot——DAX(函数)
[daily training -- Tencent selected 50] 235 Nearest common ancestor of binary search tree
Industrial Finance 3.0: financial technology of "dredging blood vessels"
[shell] clean up nohup Out file
老板总问我进展,是不信任我吗?(你觉得呢)
Simple case of SSM framework
MySQL-CentOS7通过YUM安装MySQL
Ten stages of becoming a Senior IC Design Engineer. What stage are you in now?
毕业之后才知道的——知网查重原理以及降重举例
判断文件是否为DICOM文件