//wRatio hRatio分别为Width和Height调整的百分比(%)
void ResizePicture(LPCWSTR lpSrcFile,LPCWSTR lpDstFile,int wRatio,int hRatio)
{
Image srcImg(lpSrcFile);
int srcWidth=srcImg.GetWidth();
int srcHeight=srcImg.GetHeight();
//计算调整后的Width和Height
int dstWidth=srcWidth*wRatio/100;
int dstHeight=srcHeight*hRatio/100;
// Construct a Graphics object based on the image.
Graphics imgGraphics(&srcImg);
Bitmap bitmap(dstWidth,dstHeight,&imgGraphics);
Graphics bmpGraphics(&bitmap);
bmpGraphics.DrawImage(&srcImg,0,0,dstWidth,dstHeight);
// Save the altered image.
LPWSTR lpExt=wcsrchr(lpSrcFile, L'.');
lpExt++;
LPWSTR lpEncoder;
switch(*lpExt)
{
case L 'J ':
case L 'j ':
lpEncoder=L "image/jpeg ";
break;
case L 'P ':
case L 'p ':
lpEncoder=L "image/png ";
break;
case L 'B ':
case L 'b ':
lpEncoder=L "image/bmp ";
break;
case L 'G ':
case L 'g ':
lpEncoder=L "image/gif ";
break;
case L 't ':
case L 'T ':
lpEncoder=L "image/tiff ";
break;
default:
lpEncoder=L "image/jpeg ";
}
CLSID imgClsid;
//GetEncoderClsid()函数是在MSDN中的例子
GetEncoderClsid(lpEncoder, &imgClsid);
bitmap.Save(lpDstFile,&imgClsid,NULL);
}
======================================================================================================
GetEncoderClsid函数,方便的获得编码器的CLSID。
记录该函数,方便以后查询。
int GetEncoderClsid(const WCHAR* format, CLSID* pClsid)
{
UINT num= 0;
UINT size= 0;
ImageCodecInfo* pImageCodecInfo= NULL;
GetImageEncodersSize(&num, &size);
if(size== 0)
{
return -1;
}
pImageCodecInfo= (ImageCodecInfo*)(malloc(size));
if(pImageCodecInfo== NULL)
{
return -1;
}
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;
}
}
free(pImageCodecInfo);
return -1;
}
该函数使用方法很方便,如下面的代码就分别获得5中图像编码器的CLSID
CLSID encoderClsid
GetEncoderClsid(L"image/png", &encoderClsid); //png
GetEncoderClsid(L"image/bmp", &encoderClsid);
GetEncoderClsid(L"image/gif", &encoderClsid);
GetEncoderClsid(L"image/jpeg", &encoderClsid);
GetEncoderClsid(L"image/tiff", &encoderClsid);
======================================================================================================
在GDI+ 中裁切和缩放图像
Visual Studio 2010 其他版本 .NET Framework 4.5 Visual Studio 2008 Visual Studio 2005 .NET Framework 3.5 .NET Framework 2.0
此主题尚未评级 - 评价此主题
可使用 Graphics 类的 DrawImage 方法绘制和定位矢量图像和光栅图像。 DrawImage 是一个重载的方法,因此,可以有多种方式为该方法提供参数。
DrawImage 的变体
--------------------------------------------------------------------------------
DrawImage 方法的一个变体接收一个 Bitmap 和一个 Rectangle。 该矩形指定了绘图操作的目标,即它指定了将要在其内绘图的矩形。 如果目标矩形的大小与原始图像的大小不同,原始图像将进行缩放,以适应目标矩形。 下面的代码示例演示如何将同一图像绘制三次:一次没有缩放,一次使用扩展,一次使用压缩:
C#VB复制
Bitmap myBitmap = new Bitmap("Spiral.png");
Rectangle expansionRectangle = new Rectangle(135, 10,
myBitmap.Width, myBitmap.Height);
Rectangle compressionRectangle = new Rectangle(300, 10,
myBitmap.Width / 2, myBitmap.Height / 2);
myGraphics.DrawImage(myBitmap, 10, 10);
myGraphics.DrawImage(myBitmap, expansionRectangle);
myGraphics.DrawImage(myBitmap, compressionRectangle);
下面的插图显示了这三张图片。
DrawImage 方法的一些变体带有源矩形参数和目标矩形参数。 源矩形参数指定原始图像要绘制的部分。 目标矩形参数指定将要在其内绘制该图像指定部分的矩形。 如果目标矩形的大小与源矩形的大小不同,图片将会缩放,以适应目标矩形。
下面的代码示例演示如何用文件 Runner.jpg 构造 Bitmap。 整个图像在 (0,0) 处开始绘制,无缩放。 然后将该图像的一小部分绘制两次:一次使用压缩,一次使用扩展。
C#VB复制
Bitmap myBitmap = new Bitmap("Runner.jpg");
// One hand of the runner
Rectangle sourceRectangle = new Rectangle(80, 70, 80, 45);
// Compressed hand
Rectangle destRectangle1 = new Rectangle(200, 10, 20, 16);
// Expanded hand
Rectangle destRectangle2 = new Rectangle(200, 40, 200, 160);
// Draw the original image at (0, 0).
myGraphics.DrawImage(myBitmap, 0, 0);
// Draw the compressed hand.
myGraphics.DrawImage(
myBitmap, destRectangle1, sourceRectangle, GraphicsUnit.Pixel);
// Draw the expanded hand.
myGraphics.DrawImage(
myBitmap, destRectangle2, sourceRectangle, GraphicsUnit.Pixel);
下面的插图显示了未缩放的图像,以及压缩的和扩展的图像部分。
请参见
--------------------------------------------------------------------------------
其他资源
图像、位图和图元文件
使用图像、位图、图标和图元文件