I am trying to draw a circle within my form.
But it is strange to me I set form width and height to a fixed number, i do the same for the circle, but the circle figure goes outside the form.
private void Form3_Paint(object sender, PaintEventArgs e)
{
this.SuspendLayout();
gr = this.CreateGraphics();
gr.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
Brush fill_circ3 = Brushes.Blue;
Pen ellipse_pen = new Pen(Color.Blue);
ellipse_pen.Width = (float)2.0;
this.Width = this.Height = 400;
Rectangle rect = new Rectangle(0, 0, this.Width, this.Height);
gr.DrawEllipse(ellipse_pen, rect);
this.ResumeLayout();
}
The 3rd and 4th parameters of the Rectangle constructor defines the size in width and height of the circle.
See the circle I got
Why does the circle goes outside the form???! I have set form and circle sizes the same!!!
It's because you use the Window size, not the "client" size. Just replace your code by this:
gr.DrawEllipse(ellipse_pen, this.ClientRectangle);
The client area of a control is the bounds of the control, minus the
nonclient elements such as scroll bars, borders, title bars, and
menus.
Related
I want to zoom picture box along with graphics.
this will Zoom the only image part not the graphics part.
public Image PictureBoxZoom(Image imgP, Size size)
{
Bitmap bm = new Bitmap(imgP, Convert.ToInt32(imgP.Width * size.Width), Convert.ToInt32(imgP.Height * size.Height));
Graphics grap = Graphics.FromImage(bm);
grap.InterpolationMode = InterpolationMode.HighQualityBicubic;
return bm;
}
private void zoomSlider_Scroll(object sender, EventArgs e)
{
if (zoomSlider.Value > 0 && img != null)
{
pictureBox1.SizeMode = PictureBoxSizeMode.CenterImage;
pictureBox1.Image = null;
pictureBox1.Image = PictureBoxZoom(img, new Size(zoomSlider.Value, zoomSlider.Value));
}
}
the source of image is:
img = Image.FromStream(openFileDialog1.OpenFile());
Picture is zooming but when we draw the rectangle outside image then it's not zooming along with image.
See image:
You can do this (rather) easily by scaling the e.Graphics object in the Paint event.. See here for an example!
So to work with you code you probably should add this to the Paint event:
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
g.ScaleTransform(zoom, zoom);
// now do your drawing..
// here just a demo ellipse and a line..
Rectangle rect = panel1.ClientRectangle;
g.DrawEllipse(Pens.Firebrick, rect);
using (Pen pen = new Pen(Color.DarkBlue, 4f)) g.DrawLine(pen, 22, 22, 88, 88);
}
To calculate the zoom factor you need to do a little calculation.
Usually you would not create a zoomed Image but leave that job to the PictureBox with a SizeMode=Zoom.
Then you could write:
float zoom = 1f * pictureBox1.ClientSize.Width / pictureBox1.Image.Width;
To center the image one usually moves the PictureBox inside a Panel. And to allow scrolling one sets the Panel to Autocroll=true;
But since you are creating a zoomed Image you should keep track of the current zoom by some other means.
Note:
But as you use PictureBoxSizeMode.CenterImage maybe your problem is not with the zooming after all?? If your issue really is the placement, not the size of the drawn graphics you need to do a e.Graphics.TranslateTransform to move it to the right spot..
I'm drawing a rectangle onto a Panel object (PanelArea) using the following code:
private void GenerateGraphic()
{
Graphics RandomArea = PanelArea.CreateGraphics();
RandomArea.Clear(Color.Beige);
SolidBrush Brush1 = new SolidBrush(Color.FromArgb(128,220,20,60));
Rectangle rect = new Rectangle();
//Offset is from the top left hand corner.
rect.X = 25;
rect.Y = 25;
rect.Width = 1;
rect.Height = 1;
rect.Inflate(XArea / 2, YArea / 2);
RandomArea.FillRectangle(Brush1, rect);
}
I'm setting the BackgroundImage of the Panel to a snippet of the screen.
I'd like to be able to keep the background image, whilst resetting the rectangle for it to be resized.
I need RandomArea.Clear(Color.Beige) to reset the graphics in order to remove the previous rectangle. The snag is though, is that this places the colour over the background image of the panel.
How can I clear the graphics area/rectangle each time, whilst still being able to view a background image for the Panel?
I want to make a bitmap that has a linear opacity applied. (i.e. it is more opaque on the left side and get progressively less opaque as it approaches the right.)
Is this possible? I know its possible to have a constant opacity level.
I know its possible to have a constant opacity level
So don't make it constant, LinearGradientBrush has no trouble interpolating the alpha value. A simple demonstration of a form that has the BackgroundImage set:
protected override void OnPaint(PaintEventArgs e) {
base.OnPaint(e);
var rc = new Rectangle(20, 20, this.ClientSize.Width - 40, 50);
using (var brush = new System.Drawing.Drawing2D.LinearGradientBrush(
rc,
Color.FromArgb(255, Color.BlueViolet),
Color.FromArgb(0, Color.BlueViolet),
0f)) {
e.Graphics.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.Half;
e.Graphics.FillRectangle(brush, rc);
}
}
Produced:
Put the BitMap into a PictureBox control. Then you will have to use the PictureBox's Paint event handler to do the fading. Something like
private void pictureBox_Paint(object sender, PaintEventArgs e)
{
e.Graphics.DrawImage(pictureBox.Image, 0, 0,
pictureBox.ClientRectangle, GraphicsUnit.Pixel);
Color left = Color.FromArgb(128, Color.Blue);
Color right = Color.FromArgb(128, Color.Red);
LinearGradientMode direction = LinearGradientMode.Horizontal;
LinearGradientBrush brush = new LinearGradientBrush(
pictureBox.ClientRectangle, left, right, direction);
e.Graphics.FillRectangle(brush, pictureBox.ClientRectangle);
}
This example fades the image overlay from blue to red. I am sure you can play with this to get what you want working.
I hope this helps.
I need to draw a rectangle into a panel. I dont know the color in advance, I get the color during runtime, I dont know how to set the color not to a fixed value, and second - when i try to draw the rectangle, it does nothing at all. Here is my code that should draw the rectangle (infact it does in another project, but thats just in a plain form, not into a panel)
Graphics g;
g = CreateGraphics();
Pen p;
Rectangle r;
p = new Pen(Brushes.Blue);
r = new Rectangle(1, 1, 578, 38);
g.DrawRectangle(p, r);`
So I need to replace (Brushes.Blue) with a variable and I need to draw the rectangle in a panel on its coordinates set in this code..
Construct your Pen using the Pen(Color) constructor instead of the Pen(Brush) one. Then you can define your color once you know it.
You should perform drawing in the Paint event of the panel. This event occurs whenever windows decides it's time to repaint the panel, and the PaintEventArgs contain a Graphics object you can draw the rectangle on.
The Brush is an abstract class, but you can use the SolidBrush object to create a custom colored brush at runtime:
int red = 255;
int green = 0;
int blue = 0;
Brush myBrush = new SolidBrush(Color.FromArgb(red, green, blue));
Here you are:
private Color _color; // save the color somewhere
private bool iKnowDaColor = false; // this will be set to true when we know the color
public Form1() {
InitializeComponents();
// on invalidate we want to be able to draw the rectangle
panel1.Paint += new PaintEventHandler(panel_Paint);
}
void panel_Paint(object sender, PaintEventArgs e) {
// if we know the color paint the rectangle
if(iKnowDaColor) {
e.Graphics.DrawRectangle(new Pen(_color),
1, 1, 578, 38);
}
}
And when you know the color:
_color = ...
iKnowDaColor = true;
// causes the panel to invalidate and our painting procedure to be called
panel.Invalidate();
I haven't tested this but should give you the basic idea.
put the following code in the appropriate place :
Graphics g = panel1.CreateGraphics();
int redInt=255, blueInt=255, greenInt=255; //255 is example, give it what u know
Pen p = new Pen(Color.FromArgb(redInt,blueInt,greenInt));
Rectangle r = new Rectangle(1, 1, 578, 38);
g.DrawRectangle(p, r);
and if you wanted to draw the rectangle somewhere else, say the form, you could do g = this.CreateGraphics.
I think that the better way of doing that is to extend the Panel class and add some custom OnPaint event logic.
public class PanelRect : Panel
{
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
using (Graphics g = e.Graphics)
{
Rectangle rect = ClientRectangle;
rect.Location = new Point(20, 20); // specify rectangle relative position here (relative to parent container)
rect.Size = new Size(30, 30); // specify rectangle size here
using (Brush brush = new SolidBrush(Color.Aqua)) // specify color here and brush type here
{
g.FillRectangle(brush, rect);
}
}
}
}
P.S. This is not an advanced example, but might help you. You can move size, location and color etc to properties so you can easily change them from designer.
P.S. P.S. If you need a non-filled rectangle just use Pen object instead of Brush (you might also change the FillRectangle to something more suitable).
I've been trying to mimic the Windows 7 Snipping Tool in how it overlays the screen with a semi-transparent gray layer which becomes completely transparent inside the selection area. I've come pretty close. I'm displaying a borderless 50% opaque gray form which covers the entire screen and has a TransparencyKey of Fuchsia. Then on top of that form I draw 2 rectangles. A solid fuchsia rectangle for the transparency and another rectangle for the red border. It works, but only if I do one of three things, none of which are options.
Disable double buffering which makes the form flicker while drawing
Change the desktop color mode to 16bit from 32bit
Make the form 100% opaque
Here is my code. Any suggestion on how to get this to work?
public partial class frmBackground : Form
{
Rectangle rect;
public frmBackground()
{
InitializeComponent();
this.MouseDown += new MouseEventHandler(frmBackground_MouseDown);
this.MouseMove += new MouseEventHandler(frmBackground_MouseMove);
this.Paint += new PaintEventHandler(frmBackground_Paint);
this.DoubleBuffered = true;
this.Cursor = Cursors.Cross;
}
private void frmBackground_MouseDown(object sender, MouseEventArgs e)
{
Bitmap backBuffer = new Bitmap(this.ClientSize.Width, this.ClientSize.Height);
rect = new Rectangle(e.X, e.Y, 0, 0);
this.Invalidate();
}
private void frmBackground_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
rect = new Rectangle(rect.Left, rect.Top, e.X - rect.Left, e.Y - rect.Top);
this.Invalidate();
}
private void frmBackground_Paint(object sender, PaintEventArgs e)
{
Pen pen = new Pen(Color.Red, 3);
e.Graphics.DrawRectangle(pen, rect);
SolidBrush brush = new SolidBrush(Color.Fuchsia);
e.Graphics.FillRectangle(brush, rect);
}
}
you can use
Brush brush = new SolidBrush(Color.FromArgb(alpha, red, green, blue))
where alpha goes from 0 to 255, so a value of 128 for your alpha will give you 50% opactity.
this solution was found in this question