win32 往图片中写水印

整理自己的代码文件夹发现了自己写的一个往图片中写水印的方法。恩,这等方法要写出来,差点就删掉了。\(^o^)/~貌似是自己看照公司的代码一个个的抄在笔记本上然后又一个个的敲到电脑里的,真他妈的辛苦我自己了。附上代码,有需要的尽管拿;

#pragma comment(lib,"gdiplus.lib")
#pragma once
#include "windows.h"
#include "gdiplus.h"
#include <stdio.h>
#include "Tchar.h"
#include "string.h"
#include "math.h"
#include <valarray>
using namespace std;
using namespace Gdiplus;

///some information user define ///
#define MAINFRAME      _T( "mainfram")
HWND Hwnd=NULL;
///

LRESULT CALLBACK MainFrame(HWND, UINT, WPARAM, LPARAM);


//this function purpose is to get the clsid of the image specified by me
//20121121 by cq//

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
}
///

//20121121 by cq //
/this function purpose is to add some words into the image 
/ we can adjust the ARGB(A,R,G,B) of the word A means the ALPHA, a=0 means transparent
/ here i uesed my english name "linhar" and add the  loacl time into the picture
Image* WaterMarking(WCHAR *ImageFile,int width,int height)
{
  
   /*HBITMAP pbmp;
   pbmp=LoadBitmap(
   HPALETTE hpal=NULL;
   Bitmap bmp(pbmp,hpal);
   Graphics g(&bmp);*/
	Image *image=new Image(ImageFile);

	//for the test~~~~~~~~~~~
	//printf("The width of the image is %u.\n", image->GetWidth());
    //printf("The height of the image is %u.\n", image->GetHeight());
	//~~~~~~~~~~~~~~~~~~~~~~~
	Graphics g(image);
	
	SYSTEMTIME stUTC,stLocal;
	GetSystemTime(&stUTC);
	TCHAR time[128];
	TCHAR userString[128]=L"cqtest";
	SystemTimeToTzSpecificLocalTime(NULL,&stUTC,&stLocal);

	_stprintf(time,L"%d-%d-%d %d:%d",stLocal.wYear,stLocal.wMonth,
		stLocal.wDay,stLocal.wHour,stLocal.wMinute);

	double angle   =atan2((double)height,(double)width);
	double degree  =angle*180/3.1415926;
	double chord   =sqrt((double)width*width+(double)height*height);
	double fontsize=chord/20;
	double foxPointY=height-fontsize;
	
	FontFamily fontFamily(L"Times New Roman");
	Font  foxFont(&fontFamily,(REAL)fontsize,FontStyleBold,UnitPixel);
	PointF   foxPoint((REAL)fontsize,(REAL)foxPointY);
	//PointF   foxPoint((REAL)15,(REAL)200);
	SolidBrush brush(Color(100,0,0,0));
	Matrix matrix;
	matrix.Translate(40.0f,0.0f);
	matrix.RotateAt((REAL)-degree,foxPoint,MatrixOrderAppend);
	g.SetTransform(&matrix);
	_tcscat(userString,time);
	g.DrawString(userString,15,&foxFont,foxPoint,&brush);
	
	
	CLSID Clsid;//--------------------------------------
	GetEncoderClsid(L"image/jpeg", &Clsid);
	image->Save(L"test.jpg", &Clsid);				//save the watered picture to local disc
	
return image;
	
}
///

///this function is to register a window
///20121124 by cq
bool MY_RegisterClass(WNDCLASS & wndclass)
{
	wndclass.cbClsExtra=0;
	wndclass.cbWndExtra=0;
	wndclass.hbrBackground=(HBRUSH)GetStockObject(LTGRAY_BRUSH);
	wndclass.hCursor=LoadCursor(NULL,IDC_ARROW);
	wndclass.hIcon=LoadIcon(NULL,IDI_APPLICATION);
	wndclass.hInstance=GetModuleHandle(NULL);
	wndclass.lpfnWndProc=MainFrame;
	wndclass.lpszClassName=MAINFRAME;
	wndclass.lpszMenuName=NULL;
	wndclass.style=NULL;
	
	if(!RegisterClass(&wndclass)){
	return false;
	}
	return true;

}
//
//this function purpose is to create a window register by the function  MY_RegisterClass
bool MY_CreateWindow()
{
	Hwnd=CreateWindowEx(NULL,MAINFRAME,_T("Image"),WS_SYSMENU|WS_VISIBLE,CW_USEDEFAULT,CW_USEDEFAULT, 400, 300, NULL, NULL, GetModuleHandle(NULL), NULL);
	if(!Hwnd){
		ShowWindow(Hwnd,SW_SHOWNORMAL);	
		UpdateWindow(Hwnd);
		return true;
	}
	else{
		return false;
	}
}
//

LRESULT CALLBACK MainFrame(HWND, UINT, WPARAM, LPARAM)
{
	return 0;

}

int WINAPI WinMain(
    HINSTANCE hInstance, 
    HINSTANCE hPrevInstance, 
    LPSTR lpCmdLine, 
    int nShowCmd )
{
	GdiplusStartupInput gdiplusStartupInput;
	ULONG_PTR gdiplusToken;
	GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
	
	WNDCLASS WndClass;
	if(!MY_RegisterClass(WndClass)){
		::MessageBox(NULL,_T("ERROE1"),_T("ERROR"),1);
		return 0;
	}
	else{
		if(!MY_CreateWindow()){
		::MessageBox(NULL,_T("ERROE2"),_T("ERROR"),1);
		return 0;
		}
	}

	//HWND desktop=GetDesktopWindow ();
	HDC DC=GetDC(Hwnd);
	Image *image=NULL;
	WCHAR ImageFile[30]=_T("D:\\CHENQIANG.jpg");
	Image Temp(ImageFile);
	
	Graphics g(DC);
	const RectF rect(10,10,500,500);
	image=WaterMarking(ImageFile,500,800);
	system("pause");
	g.DrawImage(image,100,10,500,875);
	
	Sleep(9000);
	ReleaseDC(Hwnd,DC);
	delete image;
	
	 MSG msg;
    while (GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return (int) msg.wParam;



	//GdiplusShutdown(gdiplusToken);
	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值