求,C#Graphics.DrawImage用法,

尤其是Graphics.DrawImage (Image, Int32, Int32, Rectangle, GraphicsUnit) ;
我试了,好几遍都发现图片没什么变化
private void button1_Click(object sender, EventArgs e)
{
Bitmap bp = new Bitmap("S1.BMP");
Graphics g = Graphics.FromImage(bp);
RectangleF destRect = new RectangleF(20, 20, 100, 100);
destRect.Width += 50;
GraphicsUnit units = GraphicsUnit.Pixel;
g.DrawImage(bp, 10, 10, destRect, units);
bp.Save("..jpg");
pictureBox1.Image =bp;
}

代码:

public void DrawImage(System.Drawing.Image image,System.

Drawing.Rectangle destRect,int srcX,int srcY,int srcWidth,int srcHeight,System.Drawing.GraphicsUnit srcUnit,System.

Drawing.Imaging.ImageAttributes imageAttrs,System.Drawing.

Graphics.DrawImageAbort callback,IntPtr callbackData);

参数

image

Image

要绘制的Image。

destRect

Rectangle

Rectangle结构,它指定所绘制图像的位置和大小。将图像进行缩放以适合该矩形。

srcX

Int32

要绘制的源图像部分的左上角的x坐标。

srcY

Int32

要绘制的源图像部分的左上角的y坐标。

srcWidth

Int32

要绘制的源图像部分的宽度。

srcHeight

Int32

要绘制的源图像部分的高度。

srcUnit

GraphicsUnit

GraphicsUnit枚举的成员,它指定用于确定源矩形的度量单位。

imageAttrs

ImageAttributes

ImageAttributes,它指定image对象的重新着色和伽玛信息。

callback

Graphics.DrawImageAbort

Graphics.DrawImageAbort委托,它指定在绘制图像期间要调用的方法。此方法被频繁调用以检查是否根据应用程序确定的条件停止

DrawImage(Image,Rectangle,Int32,Int32,Int32,Int32,GraphicsUnit,ImageAttributes,

Graphics+DrawImageAbort,IntPtr)方法的执行。

callbackData

IntPtr

一个值,它为Graphics.DrawImageAbort委托指定在检查是否停止执行DrawImage方法时要使用的附加数据。

例外

ArgumentNullException

image为null。



扩展资料:

示例

下面的代码示例旨在与Windows窗体一起使用,并且它需要PaintEventArgse,它是Paint事件处理程序的参数。

代码首先为Graphics.DrawImageAbort委托定义回调方法;定义是简化的,仅测试DrawImage方法是否使用null callBackData参数调用该定义。

示例的主体执行以下操作:

创建Graphics.DrawImageAbort回调方法的实例。

在示例的文件夹中创建SampImag JPEG文件中的图像。

创建定义要在其中绘制图像的目标矩形的点。

创建源矩形以选择要绘制的图像部分。

将图形绘图单位设置为像素。

将原始图像绘制到屏幕上。

创建一个要在其中绘制调整后的图像的附加目标矩形。

创建并设置调整后的图像的属性,使其具有比平时更大的伽玛值。

在屏幕上绘制调整后的图像。

对于原始的unadjusted目标矩形,位置在屏幕上定位图像,源矩形的大小和目标矩形的大小和形状决定了所绘制图像的缩放。

由于此示例使用传递了一个callBackData参数的重载,因此Graphics.DrawImageAbort回调返回false,这将导致DrawImage方法继续,此示例将调整后的图像绘制到屏幕上。

代码:

// Define DrawImageAbort callback method.

private bool DrawImageCallback6(IntPtr callBackData)

{

// Test for call that passes callBackData parameter.

if(callBackData==IntPtr.Zero)

{

// If no callBackData passed, abort DrawImage method.

return true;

}

else

{

// If callBackData passed, continue DrawImage method.

return false;

}

}

private void DrawImageRect4IntAtrribAbortData(PaintEventArgs e)

{

// Create callback method.

Graphics.DrawImageAbort imageCallback

= new Graphics.DrawImageAbort(DrawImageCallback6);

IntPtr imageCallbackData = new IntPtr(1);

// Create image.

Image newImage = Image.FromFile("SampImag.jpg");

// Create rectangle for displaying original image.

Rectangle destRect1 = new Rectangle(100, 25, 450, 150);

// Create coordinates of rectangle for source image.

int x = 50;

int y = 50;

int width = 150;

int height = 150;

GraphicsUnit units = GraphicsUnit.Pixel;

// Draw original image to screen.

e.Graphics.DrawImage(newImage, destRect1, x, y, width, height, units);

// Create rectangle for adjusted image.

Rectangle destRect2 = new Rectangle(100, 175, 450, 150); 

// Create image attributes and set large gamma.

ImageAttributes imageAttr = new ImageAttributes();

imageAttr.SetGamma(4.0F);

try

{

checked

// Draw adjusted image to screen.

e.Graphics.DrawImage(

newImage,

destRect2,

x, y,

width, height,

units,

imageAttr,

imageCallback,

imageCallbackData);

}

}

catch (Exception ex)

{

e.Graphics.DrawString(

ex.ToString(),

new Font("Arial", 8),

Brushes.Black,

new PointF(0, 0));

}

}



温馨提示:内容为网友见解,仅供参考
第1个回答  推荐于2016-01-06
改改吧
private void button1_Click(object sender, EventArgs e)
{
Bitmap bp = new Bitmap("S1.BMP");
//Graphics g = Graphics.FromImage(bp);改
//在pictureBox1里绘画
Graphics g = Graphics.FromHwnd(this.pictureBox1.Handle);
RectangleF destRect = new RectangleF(20, 20, 100, 100);
destRect.Width += 50;
GraphicsUnit units = GraphicsUnit.Pixel;
//从g对象(即pictureBox1)的(10,10)处开始绘制图bp的destRect区域
g.DrawImage(bp, 10, 10, destRect, units);
//bp.Save("..jpg");
//pictureBox1.Image =bp;删
}追问

我想把修改之后的图片保存怎么办?还是这个缩放只能用在显示图片上

追答

Bitmap saveBitmap = new Bitmap(1280, 1024);
g = Graphics.FromImage(saveBitmap);
g.DrawImage(bp, 50, 50, destRect, units);
saveBitmap.Save("E:\\bmp.bmp");

本回答被提问者采纳
相似回答