您如何调用这样的方法--- DrawImagePoint(PaintEventArgs g) [英] How do you call a method like this ---DrawImagePoint(PaintEventArgs g)

查看:88
本文介绍了您如何调用这样的方法--- DrawImagePoint(PaintEventArgs g)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您如何调用这样的方法

How do you call a method like this

private void DrawImagePoint(PaintEventArgs e)
{         
    // Create image.
    Image newImage = Image.FromFile("SampImag.jpg");

    // Create Point for upper-left corner of image.
    Point ulCorner = new Point(100, 100);

    // Draw image to screen.
    e.Graphics.DrawImage(newImage, ulCorner);
}

// Calling it this number of ways does not work
DrawImagePoint();
DrawImagePoint(e);

推荐答案

它是一个事件处理程序,由框架在需要绘制某些内容时调用.您不应该直接调用它.
相反,您应该调用.Invalidate()方法而不是要重绘的控件,框架将依次调用此处理程序.
It''s an event handler and it''s called by framework when something needs to be drawn. You are not supposed to call it directly.
Instead you should call .Invalidate() method no the control you wish to redraw, and framework will in turn call this handler.


给出需要使用对象作为参数.在此处看看 [
Given the method signature you need to call it with a PaintEventArgs object as the parameter. Take a look here[^] for details of what the object looks like.


作为一个经验法则,可以从某个控件的事件Paint的处理程序或其覆盖的虚拟受保护方法OnPaint调用它.在这种情况下,您传递作为事件处理程序或OnPaint方法参数传递给您的事件参数参数.更好的是,更改DrawImagePoint的参数类型并添加文件名参数,不要对其进行硬编码:

As a rule of thumb, you call it from the handler of the event Paint of some control, or its overridden virtual protected method OnPaint. In this case, you pass a event arguments parameter passed to you as the event handler or OnPaint method parameters. Better yes, change the parameter type of DrawImagePoint and add a file name parameter, don''t hard-code it:

void DrawImagePoint(System.Drawing.Graphics graphics, string fileName)
{
     Image newImage = Image.FromFile(fileName);
     Point ulCorner = new Point(100, 100);
     graphics.DrawImage(newImage, ulCorner);
} 

//...

virtual protected OnPaint(PaintEventArgs e) {
    //...
    string fileName = //... have a separate method resolving file name
    if (System.IO.File.Exists(fileName)
         DrawImagePoint(e.Graphics, fileName);
   //...
}


另外,请考虑不要从文件中加载图像(您将不得不从文件中查找图像,在未找到图像时进行处理,等等),而是从.resx资源中加载图像.在您的源代码中有一个文件.创建资源,使用添加现有文件"(最好不要使用Visual Studio将文件嵌入资源中;这种方式不利于维护.资源节点将为您(自动生成一个C#文件)(作为子节点)完成后,请查看文件内部:您将找到一个静态类,其静态属性的名称与文件名相近.例如,如果文件为.PNG,则如果此变量将是Bitmap,请键入.立即使用它;您将不需要使用资源流,创建或读取位图-没什么类似的;而是可以使用static属性,因为已经读取了文件. br/>

—SA


Also, consider loading image not from a file (you will have to find it, process cases when it is not found, etc.), but from .resx resources. Have a file in your source code. Create a resource, use "Add existing file" (better don''t embed the file in the resource using Visual Studio; this way is bad for maintenance. The resource node will make an auto-generated C# file for you (as a child node of the resource file node). When this is done, look inside this file: you will find a static class with a static property with a name close to the name of your file. For example, if your file was .PNG, the type if this variable will be Bitmap. Use it immediately; you won''t need to use resource stream, create or read a bitmap — nothing like that; instead you can use the static property as the file is already read.

—SA


这篇关于您如何调用这样的方法--- DrawImagePoint(PaintEventArgs g)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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