Windows Develop Bookmark and Share   
 index > Windows Forms General > calling methods
 

calling methods

i know that this might be a simple answer but how would you call a mehtod?

ill goive you some code to show you in what i mean:

private void btnCopy_Click(object sender, EventArgs e)

{

object CopyPixels1();

}

what do i have to put into the bracket above to call the method below? PaintEventArgs e? e?

im just well confused and would like to understand how and why for future refrence!

private void CopyPixels1(PaintEventArgs e)

{

e.Graphics.CopyFromScreen(this.Location,

new Point(40, 40), new Size(100, 100));

}

many thanx

Anonymousaaaaaaaaaaaaaaaahhhh  Tuesday, July 24, 2007 12:56 PM

First off, it you only need a Graphics to copy pixels from the screen you can easily do so by creating your own.

For instance, in your button1_Click method, you could use something like:

private void button1_Click (object sender, EventArgs e) {

// create a Bitmap to contain the portion of the screen we are about to copy

Bitmap bmp = newBitmap (200, 150); // use the size you want!!

// get a Graphics out of the bitmap

Graphics g = Graphics.FromImage (bmp);

// copy pixels from the screen... the 40, 40 is a coordinate on the screen, 0, 0, is the origin of our bitmap

g.CopyFromScreen (40, 40, 0, 0, bmp.Size);

// do whatever you want with your bitmap... for instance save it to disk.

bmp.Save ("sample.bmp");

}

This works, but has a number of problems. The main issue is that if the form is not completely visible, you can't get its pixels with Graphics.CopyFromScreen ().

Using a similar technique, you can easily set up your bitmap, get a graphics for it, create the appropriate PaintEventArgs and call OnPaintBackground and OnPaint with that. Something like:

Bitmap bmp = new Bitmap (control.Width, control.Height);

Graphics g = Graphics.FromImage (bmp);

PaintEventArgs arg = new PaintEventArgs (g, new Rectangle (0, 0, control.Width, control.Height));

OnPaintBackground (arg);

OnPaint (arg);

In a nutshell, we are instructing the control to paint on the bitmap instead of on the screen.After all thisbmp will contain what would have appeared on the screen.

Tweak as needed

HTH

--mc

Mario Cossi  Tuesday, July 24, 2007 3:59 PM

Hi Anonymous,

Try the following code :

Bitmap bmp = new Bitmap(812, 360); // use the size you want!!

//// get a Graphics out of the bitmap

Graphics g = Graphics.FromImage(bmp);

//// copy pixels from the screen... the 40, 40 is a coordinate on the screen, 0, 0, is the origin of our bitmap

////g.CopyFromScreen(40, 40, 0, 0, bmp.Size);

g.CopyFromScreen(pictureBox1.Location, new Point(6, 61), new Size(100, 100));

// do whatever you want with your bitmap... for instance save it to disk.

Clipboard.SetDataObject(bmp);// you are painting on bmp, not on this.pictureBox1.Image which is null

decyclone  Wednesday, July 25, 2007 10:11 AM
You won't be able to call CopyPixels1 method from btnCopy_Click as there is no way of casting EventArgs to PaintEventArgs. The later is only available in Paint event handler, not Click handler.

However, if you need to get the Graphics object of the control, you might want to try to use Control's CreateGraphics method and call CopyFromScreen on that Graphics object.

Another issue: why do you "call" your method with "object" in front? If CopyPixels1 is in the same class as btnCopy_Click, call it with its name. If, on the other hand, this method is in another class, then you will have to get a Graphics object in btnCopy_Click handler and pass it to that method!

HTH,
Gregor Berginc  Tuesday, July 24, 2007 2:37 PM

Why not just invoke the event that calls that function? If its on the OnPaint method for your form, then

Code Snippet
this.Invalidate();

will do just that

Tomas Scheel  Tuesday, July 24, 2007 2:52 PM

kk this.invalidate();

that repaints tothe screen and slows down the programe, that is not what i want i want to call the on paint method.

1) from a button i want to click (the button is called "copy to clipboard")

2) i need to call the CopyPixels1 what is from the onpaint method!

3)i want the onpaint method CopyPixels1 to copy the GDI+ graphical chart that iv done by code to clipboard!

gregor has the right idea, but this has all blown me away so much to the point that iv actualy lost the plott!

ahh sorry the onpaint is not on me main form its a picturebox and pre made

private void CopyPixels1(PaintEventArgs e) so i could use the graphics object!

Anonymousaaaaaaaaaaaaaaaahhhh  Tuesday, July 24, 2007 3:06 PM

could you kindly show me in what you mean in code? im new to programing in c# i dont need to be spoon fed but an example would be nice so i could fully understand!

and yes i do want the graphics object, so hard to get for somthing that shouldnt be!

many thanx

Anonymousaaaaaaaaaaaaaaaahhhh  Tuesday, July 24, 2007 3:28 PM

First off, it you only need a Graphics to copy pixels from the screen you can easily do so by creating your own.

For instance, in your button1_Click method, you could use something like:

private void button1_Click (object sender, EventArgs e) {

// create a Bitmap to contain the portion of the screen we are about to copy

Bitmap bmp = newBitmap (200, 150); // use the size you want!!

// get a Graphics out of the bitmap

Graphics g = Graphics.FromImage (bmp);

// copy pixels from the screen... the 40, 40 is a coordinate on the screen, 0, 0, is the origin of our bitmap

g.CopyFromScreen (40, 40, 0, 0, bmp.Size);

// do whatever you want with your bitmap... for instance save it to disk.

bmp.Save ("sample.bmp");

}

This works, but has a number of problems. The main issue is that if the form is not completely visible, you can't get its pixels with Graphics.CopyFromScreen ().

Using a similar technique, you can easily set up your bitmap, get a graphics for it, create the appropriate PaintEventArgs and call OnPaintBackground and OnPaint with that. Something like:

Bitmap bmp = new Bitmap (control.Width, control.Height);

Graphics g = Graphics.FromImage (bmp);

PaintEventArgs arg = new PaintEventArgs (g, new Rectangle (0, 0, control.Width, control.Height));

OnPaintBackground (arg);

OnPaint (arg);

In a nutshell, we are instructing the control to paint on the bitmap instead of on the screen.After all thisbmp will contain what would have appeared on the screen.

Tweak as needed

HTH

--mc

Mario Cossi  Tuesday, July 24, 2007 3:59 PM

kk

this is what iv got so far

Bitmap bmp = new Bitmap(812, 360); // use the size you want!!

//// get a Graphics out of the bitmap

Graphics g = Graphics.FromImage(bmp);

//// copy pixels from the screen... the 40, 40 is a coordinate on the screen, 0, 0, is the origin of our bitmap

////g.CopyFromScreen(40, 40, 0, 0, bmp.Size);

g.CopyFromScreen(pictureBox1.Location, new Point(6, 61), new Size(100, 100));

// do whatever you want with your bitmap... for instance save it to disk.

Clipboard.SetDataObject(this.pictureBox1.Image);

but there is a major problem, the last part gives off " Value cannot be null. Parameter name: data "

does that mean that it isnt copying the image to the clipboard becasue it cant see anything in the picturebox ?

Anonymousaaaaaaaaaaaaaaaahhhh  Wednesday, July 25, 2007 9:29 AM

Hi Anonymous,

Try the following code :

Bitmap bmp = new Bitmap(812, 360); // use the size you want!!

//// get a Graphics out of the bitmap

Graphics g = Graphics.FromImage(bmp);

//// copy pixels from the screen... the 40, 40 is a coordinate on the screen, 0, 0, is the origin of our bitmap

////g.CopyFromScreen(40, 40, 0, 0, bmp.Size);

g.CopyFromScreen(pictureBox1.Location, new Point(6, 61), new Size(100, 100));

// do whatever you want with your bitmap... for instance save it to disk.

Clipboard.SetDataObject(bmp);// you are painting on bmp, not on this.pictureBox1.Image which is null

decyclone  Wednesday, July 25, 2007 10:11 AM

thanx yall, and thanx dectclone for giving me the corrected code, didnt realise i was trying to paint to the picturebox not to the bmp! lol noobs mistake! anyways im geting a blue screen print but ill sort that out i hope Stick out tongue,

kudo to you all, thanx so much Smile

Anonymousaaaaaaaaaaaaaaaahhhh  Wednesday, July 25, 2007 12:43 PM

You can use google to search for other answers

Custom Search

More Threads

• Serialization of a component, which is a member
• Datagridview Date
• Open Form
• WebBrowser Javascript Issue
• Right-click selected isnt really selected
• User interface issue while sending da.update() to remove records
• How to make similar effect of auto-hide tabs?
• Append 2 byte[] arrays
• Maximize the window state of the mdi child forms
• Programatically repositioning/resizing controls on form resize