首页 > 网络基础知识

drawimage drawimage方法

Graphics.drawImage究竟是怎么个意思

public abstract boolean drawImage(Image img,

int x,

int y,

int width,

int height,

ImageObserver observer)绘制指定图像中已缩放到适合指定矩形内部的图像。

图像绘制在此图形上下文坐标空间的指定矩形内部,如果需要,则进行缩放。透明像素不影响该处已存在的像素。

此方法在任何情况下都立刻返回,甚至在整个图像没有针对当前输出设备完成缩放、抖动或转换的情况下也是如此。如果当前输出表示形式尚未完成,则 drawImage返回 false。随着更多的图像可用,加载图像的进程将通过调用图像观察者的 imageUpdate方法来通知它。

缩放的图像不一定立刻可用,因为已经针对此输出设备构造了非缩放的图像。每种大小的图像可以被分别缓存,并由各自图像生产序列中的原始数据生成。

参数:

img-要绘制的指定图像。如果 img为 null,则此方法不执行任何*作。

x- x坐标。

y- y坐标。

width-矩形的宽度。

height-矩形的高度。

observer-转换了更多图像时要通知的对象。

j*a中的paint方法中的drawimage方法怎么用

drawImage方法是用来将图片绘制到目标上

drawImage

public abstract boolean drawImage(Image img,

int x,

int y,

int width,

int height,

Color bgcolor,

ImageObserver observer)绘制指定图像中已缩放到适合指定矩形内部的图像。

图像绘制在此图形上下文坐标空间的指定矩形内部,如果需要,则进行缩放。以指定的背景色绘制透明像素。此*作等同于用给定颜色填充指定图像宽度和高度的矩形,然后在其上绘制图像,但此*作效率更高。

此方法在任何情况下都立刻返回,甚至在整个图像没有针对当前输出设备完成缩放、抖动或转换的情况下也是如此。如果当前的输出表示形式尚未完成,则 drawImage返回 false。随着更多的图像可用,加载图像的进程将通知指定的图像观察者。

缩放的图像不一定立刻是可用的,因为已经为此输出设备构造了非缩放的图像。每种大小的图像可以被分别缓存,并由各自图像生产序列中的原始数据生成。

参数:

img-要绘制的指定图像。如果 img为 null,则此方法不执行任何*作。

x- x坐标。

y- y坐标。

width-矩形的宽度。

height-矩形的高度。

bgcolor-在图像非透明部分下绘制的背景色。

observer-当转换了更多图像时要通知的对象。

一个简单的例子(drawImage方法有很多变体这里用的是最简单的一个)

import j*a.awt.BasicStroke;

import j*a.awt.BorderLayout;

import j*a.awt.Color;

import j*a.awt.Font;

import j*a.awt.Graphics;

import j*a.awt.Graphics2D;

import j*a.awt.image.BufferedImage;

import j*ax.swing.JFrame;

import j*ax.swing.JLabel;

import j*ax.swing.SwingUtilities;

public class GraphiscTest extends JLabel{

private static final long serialVersionUID=-1985867978449397006L;

public GraphiscTest(){

super();

}

@Override

public void paintComponent(Graphics g){

super.paintComponent(g);

BufferedImage image= new BufferedImage(150, 150,

BufferedImage.TYPE_3BYTE_BGR);

Graphics2D g2d= image.createGraphics();

g2d.setBackground(Color.GRAY);

g2d.clearRect(0, 0, getWidth(), getHeight());

g2d.drawString(“Default Font”, 10, 20);

g2d.drawLine(10, 22, 80, 22);

g2d.setFont(g.getFont().deriveFont(Font.BOLD| Font.ITALIC, 24f));

g2d.setColor(Color.WHITE);

g2d.setStroke(new BasicStroke(10f, BasicStroke.CAP_ROUND,

BasicStroke.JOIN_MITER));

g2d.drawString(“New Font”, 10, 50);

g2d.drawLine(10, 57, 120, 57);

g2d.dispose();

g.drawImage(image, 0, 0, null);

}

public static void main(String args[]){

SwingUtilities.invokeLater(new Runnable(){

@Override

public void run(){

JFrame frame= new JFrame();

frame.setLayout(new BorderLayout());

frame.add(new GraphiscTest(), BorderLayout.CENTER);

frame.setSize(320, 240);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setVisible(true);

}

});

}

}

求,C#Graphics.DrawImage用法,

代码:

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));

}

}

本文链接:http://www.hnxdcx.com/html/87966252.html

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件举报,一经查实,本站将立刻删除。