Restore graphic after scroll panel - c#

I'm using C# for developing a simple tool that has a picturebox inside a panel. The panel has property Autoscroll = true. If the Image of that picturebox is larger than the panel the panel has scrollbars
I could draw a rectangle on paint event of picturebox. But when i scroll, this rectangle disappears. I know it need to repaint it after moving the scrollbar but i don't know how to restore it again.
x, y, width, heigth, zoom is global variable and when use click in to treenode, it'll have data.
private void pictureBoxView_Paint(object sender, PaintEventArgs e)
{
if (choose == true)
{
Size newSize = new Size((int)(pictureBoxView.Image.Width * zoom),
(int)(pictureBoxView.Image.Height * zoom));
Graphics graphic = pictureBoxView.CreateGraphics();
Pen pen = new Pen(Color.Red, 3);
graphic.DrawRectangle(pen, x, y, width, height);
pen.Dispose();
}
}
private void treeViewTemplate_AfterSelect(object sender, TreeViewEventArgs e)
{
// refresh picturebox
pictureBoxView.Refresh();
// allow repaint
choose = true;
string[] value = treeViewTemplate.SelectedNode.Tag.ToString().Split(',');
x = Int32.Parse(value[0]);
y = Int32.Parse(value[1]);
width = Int32.Parse(value[2]);
height = Int32.Parse(value[3]);
zoom = Double.Parse(value[4]);
//MessageBox.Show("x = " + y + ", y = " + y + ", width = " + width + ", height = " + height + ", zoom = " + zoom);
// This call draw a rectangle again when I choose a value from TreeNode's Tag
pictureBoxView_Paint(this, null);
}

you also can use pictureBoxView.Refresh()
and define two local variable save the scroll offset through ScrollEventArgs.NewValue
if you do not want to paint when scrolling, you can use this
private void panel1_Paint(object sender, PaintEventArgs e)
{ pictureBox1.Refresh();}

In general you should use Invalidate method in order to redraw a surface of the control:
pictureBoxView.Invalidate();
Here is a sample of redrawing a PictureBox that is inside a Panel:
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
var rectangle = new Rectangle(10, 10, 100, 100);
e.Graphics.DrawRectangle(Pens.Red, rectangle);
}
private void panel1_Scroll(object sender, ScrollEventArgs e)
{
pictureBox1.Invalidate();
}
The fixed red rectangle is redrawed when panel is scrolled.

In this case (PictureBox in Panel control), you must create a method, which drawing on teh picture box. And you puts the drawing method in the Panel ScrollEvent.
private void panel1_Scroll(object sender, ScrollEventArgs e)
{
Drawing();
}

Related

Why can't it draw a rectangle the opposite way?

So I am trying to recreate the Snipping Tool that comes with Windows.
However I've manage to get it to draw a rectangle from the top left corner and then when I move my cursor to the bottom right.
However if I try to move my cursor from let's say the middle of the screen to the top left it doesnt draw the rectangle.
Why is this?
Here is a GIF showing what happends
https://i.imgur.com/0Y7xXnS.gifv
//These variables control the mouse position
int selectX;
int selectY;
int selectWidth;
int selectHeight;
public Pen selectPen;
//This variable control when you start the right click
bool start;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
//Hide the Form
this.Hide();
//Create the Bitmap (This is an a black bitmap holding just the size.) (Bitmap is mutable)
Bitmap printscreen = new Bitmap(Screen.PrimaryScreen.Bounds.Width,
Screen.PrimaryScreen.Bounds.Height);
//Create a graphics object that's ready for alteration. `printscreen` is now a graphics Object
Graphics graphics = Graphics.FromImage(printscreen);
//Alter the graphics object which again is the printscreen
graphics.CopyFromScreen(0, 0, 0, 0, printscreen.Size);
//Create a temporary memory stream for the image
using (MemoryStream s = new MemoryStream())
{
//save graphic variable into memory
printscreen.Save(s, ImageFormat.Bmp);
//Set the size of the picturebox
pictureBox1.Size = new Size(Width, Height);
//set the value of the picturebox.Image to an Image.FromStream and load the temp stream
pictureBox1.Image = Image.FromStream(s);
}
//Show Form
this.Show();
//Cross Cursor
Cursor = Cursors.Cross;
}
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
selectX = e.X;
selectY = e.Y;
selectPen = new Pen(Color.DimGray, 1);
selectPen.DashStyle = DashStyle.Solid;
pictureBox1.Refresh();
start = true;
}
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (start)
{
selectWidth = e.X - selectX;
selectHeight = e.Y - selectY;
pictureBox1.Refresh();
pictureBox1.CreateGraphics().DrawRectangle(selectPen,
selectX, selectY, selectWidth, selectHeight);
}
}
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
Stream sound = Resources.SnapshotSound;
SoundPlayer player = new SoundPlayer(sound);
player.PlaySync();
Application.Exit();
}
}
So what you end up with is basically a rectangle with a negative width and height.
You'll probably want to use the Math.Abs to make sure you get a proper width and height in the cases the difference would become negative.
With that, you will also probably want to draw from different positions based on where your cursor is in relation to the selected point. Something like
selectWidth = e.X - selectX;
selectHeight = e.Y - selectY;
// If width is less than 0, draw from pointer, otherwise from select X
var drawFromX = selectWidth < 0 ? e.X : selectX;
// If height is less than 0, draw from pointer, otherwise from select Y
var drawFromY = selectHeight < 0 ? e.Y : selectY;
pictureBox1.Refresh();
pictureBox1.CreateGraphics().DrawRectangle(selectPen,
drawFromX,
drawFromY,
Math.Abs(selectWidth), // Make sure the rectangle width is positive
Math.Abs(selectHeight)); // Make sure the rectangle height is positive

How can i draw a rectangle around a label control?

I added a label control to form1 designer and assign some text to it.
Then i did label mouse click event:
private void label5_MouseClick(object sender, MouseEventArgs e)
{
DrawRectangleOnLabel = true;
label5.Invalidate();
}
And the label paint event:
private void label5_Paint(object sender, PaintEventArgs e)
{
if (DrawRectangleOnLabel == true)
{
e.Graphics.DrawRectangle(Pens.Red, 0, 0, label5.Width, label5.Height);
}
}
But what i see when i click on the label is half rectangle only the Left and Top the right and bottom of the rectangle not exist/show.
This is because the rectangle is drawn with a pen width of 1 and the right and bottom portions fall outside the bounds of the label. Just make it one pixel smaller :
e.Graphics.DrawRectangle(Pens.Red, 0, 0, label1.Width - 1, label1.Height - 1);
Alternatively, you can use the ControlPaint method instead :
ControlPaint.DrawBorder(e.Graphics, label1.DisplayRectangle,
Color.Red, ButtonBorderStyle.Solid);
This gives you access to various other ButtonBorderStyles (dashed, dotted, inset, outset).
Why don't you try BorderStyle property on MouseClick event. And assign single event for all labels.
label1.MouseClick += new EventHandler(this.AllLable_MouseClick);
label2.MouseClick += new EventHandler(this.AllLable_MouseClick);
label3.MouseClick += new EventHandler(this.AllLable_MouseClick);
private void AllLable_MouseClick(object sender, MouseEventArgs e)
{
Label lbl = (Label)sender;
if (lbl.BorderStyle == BorderStyle.FixedSingle)
lbl.BorderStyle = BorderStyle.None
else
lbl.BorderStyle = BorderStyle.FixedSingle
}
For a simple "box" just add a Forms.Panel and place your controls on it. Then set the Panel border to FixedSingle.

Why text is not showing in a gradient Button?

I use the below code to set a gradient to a button in a windows form. It works but it's Text is not showing. What should I do to fix it?
Thank you.
private void Form1_Load(object sender, EventArgs e)
{
button2.Paint += new PaintEventHandler(this.Button2_Paint);
}
private void Button2_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
g.FillRectangle(new System.Drawing.Drawing2D.LinearGradientBrush(PointF.Empty, new PointF(button2.Width, button2.Height), Color.Pink, Color.Red), new RectangleF(PointF.Empty, button2.Size));
}
It's because you never actually drawing that text. Currently you only filling button's client rectangle with gradient, but there is no any text within. To show the string with button's text you need to add some more lines to Paint method:
private void Button2_Paint(object sender, PaintEventArgs e)
{
Button btn= (Button)sender;
Graphics g = e.Graphics;
g.FillRectangle(new System.Drawing.Drawing2D.LinearGradientBrush(PointF.Empty, new PointF(button2.Width, button2.Height), Color.Pink, Color.Red), new RectangleF(PointF.Empty, button2.Size));
SizeF size = g.MeasureString(btn.Text, btn.Font);
PointF topLeft = new PointF(btn.Width / 2 - size.Width / 2, btn.Height / 2 - size.Height / 2);
g.DrawString(btn.Text, btn.Font, Brushes.Black, topLeft);
}
Here Graphics.MeasureString gives you the width and height of a button's Text property, these values are used to position text right in the middle of a button. And Graphics.DrawString just draws the string with supplied color, font and position.

Draw rectangle where mouse clicked

I'm very new in C#
I want a rectangle to appear wherever there's a mouseclick on a panel
Here's my code:
private void panel1_MouseClick(object sender, MouseEventArgs e)
{
int x = e.Location.X;
int y = e.Location.Y;
if (radioButton1.Checked == false)
{
((Panel)sender).Invalidate(new Rectangle(x * 40, y * 40, 40, 40));
}
else if (radioButton2.Checked == true)
{
return;
}
}
I wonder how to change the color of the rectangle?
Please advise me if my code is wrong.
Thanks.
Your drawing should be performed in the panel's Paint event handler. When you click the panel, create the rectangle (in the MouseUp event of the panel) and store it in a collection of rectangles (such as a dictionary). Then refresh the panel. In the panel's Paint event, draw all the rectangles. Here is a simple example:
Dictionary<Color, List<Rectangle>> rectangles = new Dictionary<Color, List<Rectangle>>();
private void panel1_Paint(object sender, PaintEventArgs e)
{
//The key value for the dictionary is the color to use to paint with, so loop through all the keys (colors)
foreach (var rectKey in rectangles.Keys)
{
using (var pen = new Pen(rectKey)) //Create the pen used to draw the rectangle (using statement makes sure the pen is disposed)
{
//Draws all rectangles for the current color
//Note that we're using the Graphics object that is passed into the event handler.
e.Graphics.DrawRectangles(pen, rectangles[rectKey].ToArray());
}
}
}
//This method just adds the rectangle to the collection.
private void panel1_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
Color c = getSelectedColor(); //Gets a color for which to draw the rectangle
//Adds the rectangle using the color as the key for the dictionary
if (!rectangles.ContainsKey(c))
{
rectangles.Add(c, new List<Rectangle>());
}
rectangles[c].Add(new Rectangle(e.Location.X - 12, e.Location.Y - 12, 25, 25)); //Adds the rectangle to the collection
}
//Make the panel repaint itself.
panel1.Refresh();
}
private void panel1_MouseClick(object sender, MouseEventArgs e)
{
Graphics g = panel1.CreateGraphics();
g.DrawRectangle(new Pen(Brushes.Black),
new Rectangle(new Point(e.X, e.Y), new
Size(100, 100)));
}
you can change the color in Brushes.Black part of code, change it as you desire

C# Drawing Rectangle On the mouse event

I want to draw a rectangle. Thing what I want is that show the user to rectangle on the mouse event.
Like in the image. This is for C# .net Forms application.
Help me to achieve this. Any help is appreciated.
Thank You
Yohan
You can do that in three steps:
First check if mouse is pressed down
If it is then on mouse move event keep initializing the rectangle with new positions while mouse is being dragged
Then on paint event draw the rectangle. (It will be raised for almost every mouse event, depends mouse refresh rate and dpi)
You can do somthing like this (in your Form):
public class Form1
{
Rectangle mRect;
public Form1()
{
InitializeComponents();
//Improves prformance and reduces flickering
this.DoubleBuffered = true;
}
//Initiate rectangle with mouse down event
protected override void OnMouseDown(MouseEventArgs e)
{
mRect = new Rectangle(e.X, e.Y, 0, 0);
this.Invalidate();
}
//check if mouse is down and being draged, then draw rectangle
protected override void OnMouseMove(MouseEventArgs e)
{
if( e.Button == MouseButtons.Left)
{
mRect = new Rectangle(mRect.Left, mRect.Top, e.X - mRect.Left, e.Y - mRect.Top);
this.Invalidate();
}
}
//draw the rectangle on paint event
protected override void OnPaint(PaintEventArgs e)
{
//Draw a rectangle with 2pixel wide line
using(Pen pen = new Pen(Color.Red, 2))
{
e.Graphics.DrawRectangle(pen, mRect);
}
}
}
later if you want to check if Buttons (shown in diagram) are in rectangle or not , you can do that by checking the Button's region and check if they lie in your drawn rectangle.
The solution by Shekhar_Pro draws a rectangle just in one direction (top to bottom, left to right) if you want to draw a rectangle regardless of the mouse position and the direction of the movement the solution is:
Point selPoint;
Rectangle mRect;
void OnMouseDown(object sender, MouseEventArgs e)
{
selPoint = e.Location;
// add it to AutoScrollPosition if your control is scrollable
}
void OnMouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
Point p = e.Location;
int x = Math.Min(selPoint.X, p.X)
int y = Math.Min(selPoint.Y, p.Y)
int w = Math.Abs(p.X - selPoint.X);
int h = Math.Abs(p.Y - selPoint.Y);
mRect = new Rectangle(x, y, w, h);
this.Invalidate();
}
}
void OnPaint(object sender, PaintEventArgs e)
{
e.Graphics.DrawRectangle(Pens.Blue, mRect);
}
Those blue rectangles look a lot like controls. Drawing a line on top of a control is hard to do in Winforms. You have to create a transparent window that overlays the design surface and draw the rectangle on that window. This is also the way the Winforms designer works. Sample code is here.

Categories