How to retain existing images in a panel while redrawing in c#? - c#

I have a panel where I am drawing images on button click. But on the second button click, the previously drawn image is being replaced by the new Image.
void panel_Image_Paint(object sender, PaintEventArgs e)
{
if (Clipboard.ContainsImage())
{
Point p1 = new Point(i, 0);
e.Graphics.DrawImage(Clipboard.GetImage(), p1);
i += img.Width;
}
}
I want to retain the previously drawn image in the panel, when the new image is being drawn. The clipboard is being refreshed on each button click with the new image. Any help would be highly appreciated!!!!
Thanks..

I'm not sure how the paint event actually works and what causes your images to disappear , but what you can try is adding your images to a list and then looping through the list to get all of your images displayed on the panel.
Try this code:
int i = 0;
List<Image> Images = new List<Image>();
private void panel1_Paint(object sender, PaintEventArgs e)
{
if (Clipboard.ContainsImage())
{
Images.Add(Clipboard.GetImage());
foreach (Image item in Images)
{
e.Graphics.DrawImage(item, new Point(i,0));
i += Clipboard.GetImage().Width;
}
}
i = 0;
}
Don't forget to call the Invalidate function.
private void button1_Click(object sender, EventArgs e)
{
panel1.Invalidate();
}

Related

C# - Drag and Drop & Keep Control

So I am trying to make a drag and drop application that drags something on a panel. I did it before and I have forgot the code that I used for it. I would also like it to have an event too. Here is an example that failed to work:
private void pictureBox1_Click(object sender, EventArgs e)
{
PictureBox flower1 = new PictureBox();
flower1.Image = pictureBox1.Image;
flower1.Location = new Point(panel1.Location.X, panel1.Location.Y);
flower1.Width = 100;
this.Controls.Add(flower1);
flower1.MouseDown += new MouseEventHandler(flower1_MouseDown);
}
void flower1_MouseDown(object sender, MouseEventArgs e)
{
//flower1.Location = new Point(MousePosition.X, MousePosition.Y);
}
I wanted me to click on a flower, then it would be placed onto the panel then, if the mouse is clicked over that control duplicated onto the panel, then make the location the mouse cursors location. How would I go about doing any of this? It does not even appear to duplicate.
EDIT: Just realised that the image is underneath the panel making it not able to be seen. That's one issue, now how do I get it to drag and drop?
private void pictureBox1_Click(object sender, EventArgs e)
{
PictureBox flower1 = new PictureBox();
flower1.Image = pictureBox1.Image;
flower1.Location = Point.Empty;
flower1.Width = 100;
flower1.Parent = panel1;
flower1.MouseDown += new MouseEventHandler(flower1_MouseDown);
}

How to hide/show the background image of pictureBox?

I have a pictureBox with an image inside.
I want, when i click on a button the image should hide and click again to show the image.
In the pictureBox, using paint event i am drawing some lines.
So if im doing pictureBox1.Refresh(); it will draw the lines. I want that if i click on a button the image will not display on/off.
pictureBox1 = null; or pictureBox1.Image.Dispose(); doesn't work it's showing me big red x with white background.
To hide it:
pictureBox.Visible = false;
To hide/show it in a click event:
void SomeButton_Click(Object sender, EventArgs e)
{
pictureBox.Visible = !pictureBox.Visible;
}
For toggling the image in your PictureBox you can create a 1 pixel bitmap and assign it to the picture box when you want hide your image, then assign your image back again. I am a little unclear of what the second part of your question is asking, any drawing in the picturebox's Paint Event will remain unless you exclude it in the Paint Event based on some Condition. If you want to draw a line in the box an toggle it on/off from a button see my second example.
i.e.
public partial class Form1 : Form
{
Bitmap nullBitmap = new Bitmap(1, 1); // create a 1 pixel bitmap
Bitmap myImage = new Bitmap("Load your Image Here"); // Load your image
bool showImage; // boolean variable so we know what image is assigned
public Form1()
{
InitializeComponent();
pictureBox1.Image = myImage;
showImage = true;
}
private void button1_Click(object sender, EventArgs e)
{
if (showImage)
{
pictureBox1.Image = nullBitmap;
showImage = false;
}
else
{
pictureBox1.Image = myImage;
showImage = true;
}
}
}
Second Example
public partial class Form1 : Form
{
bool showLines;
public Form1()
{
InitializeComponent();
showLines = true;
}
private void button1_Click(object sender, EventArgs e)
{
if (showLines)
{
showLines = false;
pictureBox1.Invalidate();
}
else
{
showLines = true;
pictureBox1.Invalidate();
}
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
if(showLines)
e.Graphics.DrawLine(Pens.Purple, 0, 0, 100, 100);
}
}
picturebox1.BackgroundImage = null

How to draw points on a panel with an image

I have a panel named dPanel. I set the the backgroundImage of that panel with an image named dImage.Now I want to draw points on the panel, in other words I want to color the panel by using mouse.I want to be able to save the drawing and the image together later. My codes do this but the picture lights up during drawing and its very slow.Here is my code:
private void drawP_MouseDown(object sender, MouseEventArgs e)
{
if (!drawbool)
{
dStartPoint = e.Location;
drawbool = true;
}
drawP.Invalidate();
}
private void drawP_MouseMove(object sender, MouseEventArgs e)
{
if (drawbool)
{
dStartPoint = e.Location;
drawP.Invalidate();
}
}
private void drawP_MouseUp(object sender, MouseEventArgs e)
{
if (drawbool)
{
drawbool = false;
}
}
private void drawP_Paint_1(object sender, PaintEventArgs e)
{
if (drawbool)
{
int dStartX = dStartPoint.X;
int dStartY = dStartPoint.Y;
e.Graphics.DrawEllipse(dP, dStartX, dStartY, 2, 2);
Bitmap dPPB = new Bitmap(drawP.Width, drawP.Height);
drawP.DrawToBitmap(dPPB, new Rectangle(0, 0, drawP.Width, drawP.Height));
drawP.BackgroundImage = (Image)dPPB;
}
}
Whani's the solution? Thanks in advance.
Check out this great example about drawing on panel: Painting on a Panel.
Later you can save your drawing just by invoking panel's method DrawToBitmap.

Drawing a Bitmap?

Hey guys i'm trying to figure out how to draw a bitmap that i have loaded onto my form.
I am using this following code to draw it
private void button1_Click(object sender, PaintEventArgs e)
{
open.ShowDialog();
dir.Text = open.FileName.ToString();
image = new Bitmap(dir.Text);
e.Graphics.DrawImage(image, 85, 38);
}
Using this nothing gets drawen, am i not using the correct draw method?
I don't think you really want to paint anything yourself. Try adding a picturebox to your form, and in the button click event, simply set the image to that picturebox.
// In initializecomponent()
button1.Click += button1_Click;
// The click handler
private void button1_Click(object sender, EventArgs e)
{
if (open.ShowDialog() == DialogResult.OK)
{
dir.Text = open.FileName.ToString();
image = new Bitmap(dir.Text);
pictureBox1.Image = image;
}
}

Dragging graphics objects on top of bitmaps

I am attempting to drag a shape around a picturebox on the mousemove event but am struggling to get it to move smoothly. The picture box has an image loaded as the background and I would like the graphics object to drag a circle on top of the image when the mouse is clicked and dragged.
I have it working by creating a clone of the original image each time the mouse moves and reloading the picture box but it seems like their must be a better way to achieve this.
Without reloading the original bitmap each time any graphics added remain on the image creating a trail which is more like a paint application.
How do I clear previous drawings without reloading the entire image each time? Any help appreciated.
private void picCanvas_MouseMove(object sender, MouseEventArgs e)
{
if (_drag)
{
picCanvas.Image = (Bitmap)_original.Clone();
Graphics g = Graphics.FromImage((Bitmap)picCanvas.Image);
g.DrawEllipse(_whitePen, e.X, e.Y, 10, 10);
picCanvas.Invalidate();
}
}
private void picCanvas_MouseDown(object sender, MouseEventArgs e)
{
_drag = true;
}
private void picCanvas_MouseUp(object sender, MouseEventArgs e)
{
_drag = false;
}
check this sample it is simpler
//Load Image
Bitmap TestImage = new Bitmap(FileName);
//Create Graphics Object
Graphics g = Graphics.FromImage(TestImage);
g.DrawEllipse(new Pen(Color.Red), i, j,0.5F, 0.5F);
//View Your Results
pictureBox1.Image = TestImage;
To solve the problem in the best way, use picCanvas.Paint event.
Set the positions at mousemove event and use that positions to draw at paint event.
Point pos = Point.Empty;// or your initial position
private void picCanvas_MouseMove(object sender, MouseEventArgs e)
{
if (_drag)
{
pos = e.Location;
}
}
private void picCanvas_Paint(object sender, PaintEventArgs e)
{
if (_drag)
{
Graphics g = e.Graphics;//The event handler sends us the graphics object to use for painting
g.DrawEllipse(_whitePen, pos.X, pos.Y, 10, 10);
}
}
You should add the Paint event to the Control and set the image at formload or some initialization function.
picCanvas.Image = (Bitmap)_original.Clone();
Using the above answer from Honibis I ended up with this.
load in the image and invalidate the picture to cause a refresh
picCanvas.Image = image;
picCanvas.Invalidate()
then in the paint event
private void picCanvas_Paint(object sender, PaintEventArgs e)
{
if (_drag)
{
using (Pen pen = new Pen(Color.White, 2))
{
e.Graphics.DrawEllipse(pen, pos.X, pos.Y, 10, 10);
}
}
}

Categories

Resources