Recently began to learn the image processing knowledge, think of the previous has done to the picture to add text watermark, however, was still in the former company, time is also very long, this time when the review.
The watermark here is to add a text tag inside the picture, which is usually used to mark the copyright. To improve reusability, I encapsulated the implementation of this feature into a class.
Implementation principle:
1. Load a bitmap into memory
2. Create a memory device context that is compatible with the screen memory device MEMDC
3. Select the bitmap into the MEMDC
4. Set Custom font fonts
5. Select the font into the MEMDC
6. Set MEMDC textcolor (text color), bkcolor (text background color), transparent (text background is transparent)
7. Automatically or manually set the position of the text in the picture
8. Use the TextOut function to output watermark text to a memory device MEMDC
9. Save the contents of the MEMDC in place in the map file
Code implementation:-Class Cwatermark
Header file
/* Copyright (C) wuchen all RIGHTS RESERVED.
* Author:niewucai
* email:niewucai@126.com
* Last UPDATE:2011/05/11
* Implementation of the bitmap image to add text watermark function-header file
*/
#pragmaonce
#include <string>
UsingNamespace std;
Class cwatermark{
Public
/* Construction method
* @lpszImgFile: Bitmap picture file to add a watermark
*/
Cwatermark (LPCTSTR lpszimgfile);
/* Construction method
* @nIDResource: Bitmap resource identification
*/
Cwatermark (UINT nIDResource);
~cwatermark ();
Watermark Text Position Enumeration
Typedefenum
{
/* Represents the upper-left position.
*/
Lefttop = 1,
/* Indicates an upper-middle position.
*/
Centertop,
/* represents the upper right position.
*/
Righttop,
/* Represents the left middle position.
*/
Leftmiddle,
/* Represents a central location.
*/
Centermiddle,
/* Represents the right middle position.
*/
Rightmiddle,
/* Represents the bottom left position.
*/
Leftbottom,
/* represents the middle and lower position.
*/
Centerbottom,
/* represents the lower right position.
*/
Rightbottom
}location;
Public
int m_topspacing; The amount of space (in pixels) between the watermark text and the top of the picture
int m_rightspacing; The amount of space (in pixels) between the watermark text and the right end of the picture
int m_bottomspacing;//The amount of space (pixel) between the watermark text and the bottom of the picture
int m_leftspacing; The amount of space (in pixels) between the watermark text and the left side of the picture
Public
/* Replace the current operation bitmap
* @nIDResouce: Bitmap resource identification
* Return: Whether the replacement is successful
*/
BOOL Changebitmap (UINT nIDResource);
/* Replace the current operation bitmap
* @lpszImgFile: Bitmap file path
* Return: Whether the replacement is successful
*/
BOOL Changebitmap (LPCTSTR lpszimgfile);
/* Get a device-independent memory device context * *
Inline HDC GETMEMORYDC ()
{
return M_HMEMDC;
}
/* Get the bitmap structure of the current processing bitmap * *
int Getbitmap (bitmap* pbitmap);
/* Get the foreground color of the watermark text * *
COLORREF GetForeColor ();
/* Set the foreground color of the watermark text * *
void Setforecolor (Colorref foreColor = 0);
/* Get the background color of the watermark text * *
COLORREF GetBkColor ();
/* Set the background color of the watermark text * *
void SetBkColor (Colorref bkcolor = RGB (255, 255, 255));
/* Gets whether watermark text is currently set to transparent background * *
BOOL Istransparent ();
/* Set whether watermark text is currently set to transparent background * *
void Settransparent (bool transparent = true);
/* Get the font that is currently used to print the watermark text * *
Hfont Gettextfont ();
/* Sets the font that is currently used to output watermark text, and returns the font that was last set * *
Hfont Settextfont (Hfont hfont);
/* Add watermark Text
* @lpszText: Watermark Text
* @x: Watermark text relative to the upper-left corner of the picture in the horizontal position (pixel)
* @y: The vertical position of the watermark text relative to the upper-left corner of the picture (pixel)
*/
void Addwatermark (LPCTSTR lpsztext, int x, int y);
/* According to the choice of the way and up and down the spacing, automatically add watermark text to the appropriate location, the default to add to the picture at the bottom right corner * *
void Addwatermark (LPCTSTR lpsztext, Location type = Location::rightbottom);
/* Save to the current operation bitmap file, return whether save success-only for bitmap file loading
BOOL Save ();
/* Save to the specified bitmap file, return to save success/
BOOL Save (LPCTSTR lpszfile);
Private
/* Initialization Work/*
void Init ();
/* Create bitmap file information according to the bitmap handle * *
Pbitmapinfo Createbitmapinfostruct (HWND hwnd, HBITMAP hbmp);
/* Create bitmap file * *
void Createbmpfile (HWND hwnd, LPCTSTR Pszfile, Pbitmapinfo PBI, Hbitmap hbmp, HDC HDC);
Private
String M_imgfile; Picture file to add a watermark
Hfont M_hfont; Current font for watermark text
Hfont M_holdfont;
Hfont M_hinitfont; Initial font for watermark text
COLORREF M_forecolor; Text Color of watermark text
&