我可以使用这些代码将图形制作成图像吗? [英] Can i make graphic into a image using these code?

查看:74
本文介绍了我可以使用这些代码将图形制作成图像吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public void drawPoint(int x,int y,SolidBrush brush)

{

Graphics g = Graphics.FromHwnd(pictureBox1.Handle);

Point dPoint = new Point(x,(pictureBox1.Height - y));

dPoint.X = dPoint.X - 2;

dPoint.Y = dPoint.Y - 2;

Rectangle rect = new Rectangle(dPoint,new Size(5,5));

g.FillRectangle(brush,rect);

g.Dispose();



}



int a = 0;

int b = 0;



private void pictureBox1_Paint(object sender,System.Windows.Forms.PaintEventArgs e)

{

使用(Graphics graphic = pictureBox1.CreateGraphics())

{

graphic.DrawImage(pictureBox1.Image,e。 ClipRectangle,a,b,e.ClipRectangle.Width,

e。 ClipRectangle.Height,GraphicsUnit.Pixel);

//e.Graphics.DrawImage(pictureBox1.Image,e.ClipRectangle,a,b,e.ClipRectangle.Width,e.ClipRectangle.Height,GraphicsUnit .Pixel);

}

}

public void drawPoint(int x, int y, SolidBrush brush)
{
Graphics g = Graphics.FromHwnd(pictureBox1.Handle);
Point dPoint = new Point(x, (pictureBox1.Height - y));
dPoint.X = dPoint.X - 2;
dPoint.Y = dPoint.Y - 2;
Rectangle rect = new Rectangle(dPoint, new Size(5, 5));
g.FillRectangle(brush, rect);
g.Dispose();

}

int a = 0;
int b = 0;

private void pictureBox1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
using (Graphics graphic = pictureBox1.CreateGraphics())
{
graphic.DrawImage(pictureBox1.Image, e.ClipRectangle, a, b, e.ClipRectangle.Width,
e.ClipRectangle.Height, GraphicsUnit.Pixel);
//e.Graphics.DrawImage(pictureBox1.Image, e.ClipRectangle, a, b, e.ClipRectangle.Width,e.ClipRectangle.Height, GraphicsUnit.Pixel);
}
}

推荐答案

没有。图像句柄与图形无关。而不是使用 Graphics.FromHwnd ,使用 Graphics.FromImage

http://msdn.microsoft.com/en-us/library/system.drawing.graphics。 fromimage.aspx [ ^ ]。



它将为您提供 System.Drawing.Graphics 的实例可以用来绘制位图。



另外,使用类 PictureBox 可能是毫无意义的。它只是一个控件,而不是图像。图像是它的财产。在大多数情况下,您需要使用图像本身,而不是其包含控件。我在过去的答案中解释过:

附加一个图片框内的图片 [ ^ ],

画一个C#中的矩形 [ ^ ],

如何从旧图纸中清除面板 [ ^ ]。



另见我过去的答案在控件和一些背景中渲染图形:

在面板上捕获图纸 [ ^ ],

什么样的俏皮方法是Paint? (DataGridViewImageCell.Paint(...)) [ ^ ],

mdi子表单之间的绘制线 [ ^ ],

如何加速我的vb.net应用程序? [ ^ ],

用C#.net鼠标滚轮缩放图像 [ ^ ]。



-SA
No. Image handle is unrelated to the graphics. Instead of using Graphics.FromHwnd, use Graphics.FromImage:
http://msdn.microsoft.com/en-us/library/system.drawing.graphics.fromimage.aspx[^].

It will give you the instance of System.Drawing.Graphics you can use to draw on a bitmap.

Also, it's possible that using the class PictureBox is pointless. It it just a control, not the image. Image is its property. In most cases, you need to use the image itself, not its containing control. I explained it in my past answers:
Append a picture within picturebox[^],
draw a rectangle in C#[^],
How do I clear a panel from old drawing[^].

See also my past answers on rendering graphics in controls and some background:
capture the drawing on a panel[^],
What kind of playful method is Paint? (DataGridViewImageCell.Paint(...))[^],
Drawing Lines between mdi child forms[^],
How to speed up my vb.net application?[^],
Zoom image in C# .net mouse wheel[^].

—SA


这篇关于我可以使用这些代码将图形制作成图像吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆