GDI图像基本操作

17 篇文章 3 订阅
14 篇文章 0 订阅

GDI图像基本操作:

借鉴自:http://blog.csdn.net/harvic880925/article/details/9129011

实例:

void CMFC_GDI_TESTDlg::OnLoadImg()
{
	// 基本变换
	CDC* cdc = GetWindowDC();
	Gdiplus::Graphics graphics(cdc->GetSafeHdc());
	graphics.Clear(Color(255,255,255,255));  
	Gdiplus::Image image(L"1.png");
	UINT width=image.GetWidth();  
	UINT height=image.GetHeight(); 
 
	//原图显示
	graphics.TranslateTransform(0, 60);  //平移来改变图像平面坐标,使其从此坐标重新开始绘图
	graphics.DrawImage(&image, RectF(0, 0, width, height));//显示image,使用RectF进行缩放

	//缩小一半显示
	graphics.TranslateTransform(width+20, 0);  //平移来改变图像平面坐标,使其从此坐标重新开始绘图
	graphics.DrawImage(&image,RectF(0, 0,width/2, height/2)); 

	//放大一倍显示
	graphics.TranslateTransform(width/2+20, 0);  
	graphics.DrawImage(&image,RectF(0, 0,2*width, 2*height),0,0,width,height,UnitPixel); //40,10为图像截取的宽和高,
	//在坐标系平移后的坐标点上,绘制大小宽为2*width,长为2*height,显示的内容是在原图width+40,height+10的基础上截取后平铺显示

	//使用插补模式控制图形缩放质量
	//高质量双三次插值法  这种方法是内存消耗最大
	graphics.TranslateTransform(2*width+20, 0);  
	graphics.SetInterpolationMode(InterpolationModeHighQualityBicubic);//高质量双三次插值法  
	graphics.DrawImage(&image,RectF(0,0,2*width,2*height),20,20,width/2,height/2,UnitPixel); 

	//最临近插值法 只有这种方法会产生马赛克效果,但是资源使用最小
	graphics.TranslateTransform(2*width+20, 0);  
	graphics.SetInterpolationMode(InterpolationModeNearestNeighbor);//最临近插值法  
	graphics.DrawImage(&image,RectF(0,0,2*width,2*height),20,20,width/2,height/2,UnitPixel); 

	//旋转 
	Matrix matrix;  //矩阵变换
	graphics.GetTransform(&matrix); 
	graphics.SetTransform(&matrix);  
	graphics.ResetTransform();//重置坐标时序,从左上角原点开始

	//原图   
	graphics.TranslateTransform(0, 2*width+60+20);  
	graphics.DrawImage(&image,RectF(0, 0, width, height));

	//不旋转 水平翻转 
	graphics.TranslateTransform(width+20, 0);  
	image.RotateFlip(RotateNoneFlipX); //不旋转 水平翻转   
	graphics.DrawImage(&image,RectF(0, 0, width, height));

	//旋转90度 不翻转  
	graphics.TranslateTransform(width+20, 0);  
	image.RotateFlip(Rotate90FlipNone); //旋转90度不翻转  
	graphics.DrawImage(&image,RectF(0, 0, width, height));

	//旋转90度 水平翻转  
	graphics.TranslateTransform(width+20, 0);  
	image.RotateFlip(Rotate90FlipX); //旋转90度水平翻转
	graphics.DrawImage(&image,RectF(0, 0, width, height));

	//缩略图
	graphics.ResetTransform();//重置坐标时序,从左上角原点开始
	image.RotateFlip(Rotate180FlipNone); //先把原图还原,不然会使用上面变换过的图片填充,由于上面翻转过,所以使用反向翻转还原
	graphics.TranslateTransform(0, 2*width+60 +20 +width+20);  
	float shear=image.GetHeight()/image.GetWidth();//获取高/宽比,以至缩放时不变形  
	Image *pThumbnail=image.GetThumbnailImage(0.5*width,height,NULL,NULL);//使用0.5倍宽度*高度的原图来填充下面的椭圆

	TextureBrush picBrush(pThumbnail);  
	graphics.FillEllipse(&picBrush,RectF(0,0,400,200));  

	//克隆图片 是GDI+ 特有的概念 Bitmap类可以实现局部克隆和整体克隆,而Image类只能整体克隆
	//在这儿通过克隆 拆分图片成4部分
	graphics.ResetTransform();//重置坐标时序,从左上角原点开始
	graphics.TranslateTransform(420, 2*width+60 +20 +width+20);  

	Bitmap bmp(L"1.png");  
	INT bmpheight=bmp.GetHeight();  
	INT bmpwidth=bmp.GetWidth();  

	RectF dest[4];  
	dest[0]=RectF(0,   0,						bmpwidth/2, bmpheight/2);  
	dest[1]=RectF(bmpwidth/2,  0,				bmpwidth/2, bmpheight/2);  
	dest[2]=RectF(0,   bmpheight/2,			bmpwidth/2, bmpheight/2);  
	dest[3]=RectF(bmpwidth/2, bmpheight/2,	bmpwidth/2, bmpheight/2); 

	Bitmap *s[4];  
	s[0]=bmp.Clone(dest[0],PixelFormatDontCare);//拷贝bmp图片的局部
	s[1]=bmp.Clone(dest[1],PixelFormatDontCare);  
	s[2]=bmp.Clone(dest[2],PixelFormatDontCare);  
	s[3]=bmp.Clone(dest[3],PixelFormatDontCare);  
	//绘图  
	graphics.DrawImage(s[0],dest[0]);  
	graphics.DrawImage(s[1],RectF(bmpwidth/2+10,0,bmpwidth/2,bmpheight/2));  
	graphics.DrawImage(s[2],RectF(0,bmpheight/2+10,bmpwidth/2,bmpheight/2));  
	graphics.DrawImage(s[3],RectF(bmpwidth/2+10,height/2+10,bmpwidth/2,bmpheight/2));  
	ReleaseDC(cdc);
}


void CMFC_GDI_TESTDlg::OnZoomImg()
{
	// 投射与倾斜
	CDC* cdc = GetWindowDC();
	Gdiplus::Graphics graphics(cdc->GetSafeHdc());
	//投射与倾斜
	graphics.Clear(Color::Green);  //使用绿色来刷新背景
	graphics.SetInterpolationMode(InterpolationModeHighQualityBicubic);  
	Image img(L"1.png");  
	INT left=100;  
	INT top=200;  
	INT width=img.GetWidth();  
	INT height=img.GetHeight();  
	for (int x=1; x<20; x++)
	{
		graphics.Clear(Color::Green);  //移动一次,刷新一次背景,使用绿色来刷新背景

		graphics.TranslateTransform(x, 1);  //不断变换坐标 使其呈现位移的效果
		//正面  
		PointF desFace[]={  
			PointF(left,top),  
			PointF(left+width,top),  
			PointF(left,top+width),  
		};  
		graphics.DrawImage(&img,desFace,3);  
		//上面投射  
		PointF destTop[]={  //投射的图像矩形的三个坐标点需要我们自己计算给出
			PointF(left+width/4, top-width/4),  
			PointF(left+width/4+width,top-width/4),  
			PointF(left,top)
		};  
		graphics.DrawImage(&img,destTop,3);  
		//侧面投射  
		PointF desRight[]={  
			PointF(left+width,top),  
			PointF(left+width/4+width,top-width/4),  
			PointF(left+width,top+width)  
		};  
		graphics.DrawImage(&img,desRight,3);  
		Sleep(300);
	}
	ReleaseDC(cdc);
}


void CMFC_GDI_TESTDlg::OnReduceImg()
{
	// 色彩变换
	CDC* cdc = GetWindowDC();
	Gdiplus::Graphics graphics(cdc->GetSafeHdc());
	graphics.TranslateTransform(0, 60);  //平移来改变图像平面坐标,使其从此坐标重新开始绘图
	graphics.Clear(Color(255,255,255,255));  
	Image img(L"1.png");  
	int width=img.GetWidth();
	int height=img.GetHeight();
	//原图
	graphics.DrawImage(&img,RectF(0,0,width,height));

	//应用色彩变换
	graphics.TranslateTransform(width+10,0);
	ColorMatrix colorMatrix={//色彩矩阵
		0.0f,1.0f,0.0f,0.0f,0.0f,
		0.0f,1.0f,0.0f,0.0f,0.0f,
		0.0f,0.0f,1.0f,0.0f,0.0f,
		0.0f,0.0f,0.0f,1.0f,0.0f,
		0.0f,0.0f,0.0f,0.0f,1.0f
	};

	ImageAttributes imgattr;
	//使用上面色彩矩阵绘图
	imgattr.SetColorMatrix(&colorMatrix,ColorMatrixFlagsDefault,ColorAdjustTypeBitmap);
	graphics.DrawImage(&img,RectF(0,0,img.GetWidth(),img.GetHeight()),0,0,img.GetWidth(),img.GetHeight(),UnitPixel,&imgattr);

	//临时清除对图像色彩的变换
	imgattr.SetNoOp(ColorAdjustTypeBitmap);
	graphics.TranslateTransform(width+10,0);
	graphics.DrawImage(&img,RectF(0,0,img.GetWidth(),img.GetHeight()),0,0,img.GetWidth(),img.GetHeight(),UnitPixel,&imgattr);

	//撤销的上面的清除操作
	imgattr.ClearNoOp(ColorAdjustTypeBitmap);
	graphics.TranslateTransform(width+10,0);
	graphics.DrawImage(&img,RectF(0,0,img.GetWidth(),img.GetHeight()),0,0,img.GetWidth(),img.GetHeight(),UnitPixel,&imgattr);
	ReleaseDC(cdc);
}


void CMFC_GDI_TESTDlg::OnSpinImg()
{
	// 设置透明色范围,可用于范围抠图
	CDC* cdc = GetWindowDC();
	Gdiplus::Graphics graphics(cdc->GetSafeHdc());
	graphics.TranslateTransform(0, 60);  //平移来改变图像平面坐标,使其从此坐标重新开始绘图
	graphics.Clear(Color(255,0,255,0));  
	Image img(L"2.png");
	//绘制原图
	graphics.DrawImage(&img,RectF(0,0,img.GetWidth(),img.GetHeight()));
	//设置透明色范围
	graphics.TranslateTransform(img.GetWidth()+10,0);
	ImageAttributes imgAttributes;
	imgAttributes.SetColorKey(Color(0,100,200,200),Color(0,255,255,255),ColorAdjustTypeBitmap);
	graphics.DrawImage(&img,RectF(0,0,img.GetWidth(),img.GetHeight()),0,0,img.GetWidth(),img.GetHeight(),UnitPixel,&imgAttributes);
	ReleaseDC(cdc);
}


实际效果:







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值