473,387 Members | 1,481 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Help with PictureBox and graphics

I am making a program that needs to put a copy of a certain small image
wherever the user clicks with the mouse. I am using a pictureBox as my main
area (although I can change this if necessary). How can I paste my image in
various places on the pictureBox?

I thought it might be something like this...?

PasteImage(x,y);
PasteImage(x+100,y+100);

Thanks!!
Nov 17 '05 #1
4 6829
Here's one idea on how this could be done.

Store the Points where the user clicked in some type of list (ArrayList, for
example) and then paint the images from within a Paint event handler, or
OnPaint override for a custom control. Using a Panel as the canvas, it would
go something like this.

private ArrayList imageLocations = new ArrayList();
private Image image = Image.FromFile(@"C:\Image.bmp");

private void panel1_Click(object sender, System.EventArgs e)
{
Point point = this.panel1.PointToClient(Control.MousePosition);
this.imageLocations.Add(point);
this.panel1.Invalidate(new Rectangle(point.X, point.Y, this.image.Width,
this.image.Height));
}

private void panel1_Paint(object sender, System.Windows.Forms.PaintEventArgs
e)
{
foreach (Point point in this.imageLocations)
{
e.Graphics.DrawImage(this.image, point.X, point.Y);
}
}

The Panel area can be reset using the following code.

this.imageLocations.Clear();
this.panel1.Invalidate();

--
Tim Wilson
..Net Compact Framework MVP

"Keith Smith" <ke*********@verizon.net> wrote in message
news:7IH1e.27714$b_6.15415@trnddc01...
I am making a program that needs to put a copy of a certain small image
wherever the user clicks with the mouse. I am using a pictureBox as my main area (although I can change this if necessary). How can I paste my image in various places on the pictureBox?

I thought it might be something like this...?

PasteImage(x,y);
PasteImage(x+100,y+100);

Thanks!!

Nov 17 '05 #2
> Store the Points where the user clicked in some type of list (ArrayList,
for
example) and then paint the images from within a Paint event handler, or
OnPaint override for a custom control. Using a Panel as the canvas, it
would
go something like this.


Would this method add the image to the panel as a graphic? Or would I have
to make the app remember where each of the points are for the next time it
pulls up the panel?
Nov 17 '05 #3
The ArrayList is storing the Points and everytime the Panel paints its
entire surface it would need to paint the images at those indicated
locations. If you're looking to save the image on the Panel then you could
do something similar to the following.

using System.Drawing.Imaging;

private Bitmap bmp = null;

public Form1()
{
InitializeComponent();
SizeBitmap();
}

private void panel1_Paint(object sender, System.Windows.Forms.PaintEventArgs
e)
{
Graphics graphic = Graphics.FromImage(this.bmp);
graphic.Clear(this.panel1.BackColor);
foreach (Point point in this.imageLocations)
{
graphic.DrawImage(this.image, point.X, point.Y);
}
e.Graphics.DrawImage(this.bmp, 0, 0);
graphic.Dispose();
}

private void panel1_Resize(object sender, System.EventArgs e)
{
SizeBitmap();
}

private void SizeBitmap()
{
if (this.bmp != null)
{
this.bmp.Dispose();
}
this.bmp = new Bitmap(this.panel1.Width, this.panel1.Height);
}

private void SaveBitmap()
{
if (this.bmp != null)
{
this.bmp.Save(@"C:\SavedImage.gif", ImageFormat.Gif);
}
}

Now the images are being added to the Panel as one graphic.

--
Tim Wilson
..Net Compact Framework MVP

"Keith Smith" <ke*********@verizon.net> wrote in message
news:1tV1e.26122$uw6.18197@trnddc06...
Store the Points where the user clicked in some type of list (ArrayList,
for
example) and then paint the images from within a Paint event handler, or
OnPaint override for a custom control. Using a Panel as the canvas, it
would
go something like this.
Would this method add the image to the panel as a graphic? Or would I

have to make the app remember where each of the points are for the next time it
pulls up the panel?

Nov 17 '05 #4
As Tim suggest, you can store the history of drawable objects in an
array list.
You can even serialize the array list.

http://www.geocities.com/jeff_louie/OOP/oop19.htm

Regards,
Jeff
I am making a program that needs to put a copy of a certain small image
wherever the user clicks with the mouse.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Otie | last post by:
I have a circle that displays information in 1-degree increments - there are 90 such increments that comprise the circle. I need a pointer within the circle that rotates. The pointer must include...
0
by: Pudibund | last post by:
Ok, I've spent nearly a week trying to sort what should be an easy task to accomplish but I'm totally flumoxed! I want to do something pretty simple... 1. display image1 2. wait until...
12
by: yaya via DotNetMonster.com | last post by:
Hi, I have a picture box with circles and rectangles, and I wana save all the images into a jpg file, i tried pictureBox1.Image.Save(@"c:\1.jpg"); but I got and error...
0
by: SamSpade | last post by:
I have a usercontrol that contains a picturebox. The user can obtain (creategraphics) a picturebox graphics object and draw on the picturebox. She could do gr.GraphincInit.Millimeter and then draw...
2
by: Ashley | last post by:
Please help this vb.net newbie! I would like to draw a graphics primitive. For example, a line, and have that line be it's own object (so that I can have a collection of lines) and have that line...
1
by: John | last post by:
I use drawline and drawstring to create something in a picturebox and want to save the content of that picturebox as a bitmap, is that possible and how? Thank.
2
by: Overseer | last post by:
What is the best way to draw graphics like Task Manager's Performance Graphic??
3
by: sin | last post by:
Hello, I have small problem, here's the situation: There are a windows form - Form1; And two picturebox'es - pictureBox1 and pictureBox2; Those two pictures are transparent in some areas; I...
10
by: eddie tan | last post by:
Hi, I have a picturebox with graphics drawn from different objects. In one object I used Pen P1; P1 = new Pen(Color.Blue, 3); Graphics g = null; g = Graphics.FromImage(image.Image);...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.