Click here to Skip to main content
15,899,026 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
How do you call a method like this

C#
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);
Posted
Updated 5-Jun-12 6:50am
v4
Comments
Gregory Gadow 5-Jun-12 12:35pm    
What is the actual problem you are having?
MuhtarQong 5-Jun-12 12:51pm    
Are you trying to display (draw) the image from the point of ulCorner, or do you want to put a point at ulCorner [point(100,100)] on the "newImage" ? Please make it clear.
Al Yambo 5-Jun-12 13:01pm    
Trying to display the image at point ulCorner.
VallarasuS 5-Jun-12 13:08pm    
Unless you have any special needs [may be building a custom control] I would recommend you use a picture box. further by the dont read the file every-time you paint, paint event meant to occur thousands of times. Read it once and hash it.
MuhtarQong 5-Jun-12 13:10pm    
Then, you are doing correct in terms of drawing image. However, please make a reference other opinions how do you call the function. Your code draw the image from the point of your ulCorner. Please take a look at SA's comment. Do not hard code and give it ulCorner.X and ulCorner.Y values as [ e.Graphics.DrawImage(newImage, new Point(ulCorner.X, ulCorner.Y)); ]

this.pictureBox1.SetBounds(ulCorner.X, ulCorner.Y, newImage.Width, newImage.Height);
this.pictureBox1.Image = newImage;

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.
 
Share this answer
 
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.
 
Share this answer
 
Comments
Al Yambo 5-Jun-12 14:09pm    
Thanks. Good read.
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:

C#
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);
   //...
}

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
 
Share this answer
 
Comments
VallarasuS 5-Jun-12 13:05pm    
I agree with this solution but unless it has a special purpose why dont you use a picture box and place it at the point!?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900