There is an ellipse drawn with the following code:
graphGraphics = e.Graphics;
graphGraphics.FillEllipse(new SolidBrush(Color.White), this.graphBoundries);
graphGraphics.DrawEllipse(graphPen, this.graphBoundries);
I have a line on this graph and it currently just passes right through it. I want to change the lines height to adjust to the ellipse's boundaries as follows so it wont pass through the ellipse:
http://i1379.photobucket.com/albums/ah134/fac7orx2/circlewithlinehelp_zps280d9e76.png
Does anyone know an algorithm to do this? Or maybe even how to just get the ellipse's boundaries and not just the rectangular boundaries?
To expand on my comment, try something like this (untested) code:
graphGraphics = e.Graphics;
graphGraphics.FillEllipse(new SolidBrush(Color.White), this.graphBoundries);
graphGraphics.DrawEllipse(graphPen, this.graphBoundries);
GraphicsPath clipPath = new GraphicsPath();
clipPath.AddEllipse(this.graphBoundaries);
graphGraphics.SetClip(clipPath, CombineMode.Replace);
// draw your line
Related
I want to use a PictureBox as a canvas and draw some text on it and save.
I wrote this piece of code but I'm not sure if im doing this the correct way:
Bitmap b = new Bitmap(pictureBox1.Width, pictureBox1.Height);
Graphics g = Graphics.FromImage(b);
g.FillRectangle(new SolidBrush(Color.White), new Rectangle(0, 0, pictureBox1.Width, pictureBox1.Height)); // i used this code to make the background color white
g.DrawString("some text", new Font("Times New Roman", 20), new SolidBrush(Color.Red), new PointF(10, 10));
pictureBox1.Image = b;
This code works well but when I want to change the background color of the image I have to redraw the text.
Is there a way to change the background color without having to redraw the text?
Writing a Paint program is a lot of fun, but you need to plan ahead for all or most of the features you want.
So far you have these:
A background you can change
A way to modify an image by drawing text on it
The need to save it all to a file
Here are a few more things you will need:
Other tools than just text, like lines, rectangles etc..
A choice of colors and pens with widths
A way to undo one or more steps
Here are few thing that are nice to have:
A way to help with drawing and positioning with the mouse
Other type backgrounds like a canvas or pergament paper
The ability to draw with some level of tranparency
A redo feature (*)
Rotation and scaling (***)
Levels (*****)
Some things are harder (*) or a lot harder (***) than others, but all get hard when you decide to patch them on too late..
Do read this post (starting at 'actually') about PictureBoxes, which explain how it is the ideal choice for a Paint program.
Your original piece of code and your question have these problems:
You seem to think that repeating anything, like redrawing the text is wrong. It is not. Windows redraws huge numbers of things all the time..
You have mixed two of the tasks which really should be separate.
You have not parametrizied anything, most notably the drawing of the text should use several variables:
Font
Brush
Position
the text itself
The same will be true once you draw lines or rectangles..
So here are the hints how do get it right:
Use the BackgroundColor and/or the BackgroundImage of the Picturebox to dynamically change the background!
Collect all things to draw in a List<someDrawActionclass>
Combine all drawings by drawing it into he Picturebox's Image
Use the Paint event to draw supporting things like the temporary rectangle or line while moving the mouse. On MouseUp you add it to the list..
So, coming to the end, let's fix your code..:
You set the backgound with a function like this:
void setBackground(Color col, string paperFile)
{
if (paperFile == "") pictureBox1.BackColor = col;
else pictureBox1.BackgroundImage = Image.FromFile(paperFile);
}
you can call it like this: setBackground(Color.White, "");
To draw a piece of text into the Image of the PictureBox, first make sure you have one:
void newCanvas()
{
Bitmap bmp = new Bitmap(pictureBox1.ClientSize.Width, pictureBox1.ClientSize.Height);
pictureBox1.Image = bmp;
}
Now you can write a function to write text. You really should not hard-code any of the settings, let alone the text! This is just a quick and very dirty example..:
void drawText()
{
using (Font font = new Font("Arial", 24f))
using (Graphics G = Graphics.FromImage(pictureBox1.Image))
{
// no anti-aliasing, please
G.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SingleBitPerPixel;
G.DrawString("Hello World", font, Brushes.Orange, 123f, 234f);
}
pictureBox1.Invalidate();
}
See here and here for a few remarks on how to create a drawAction class to store all the things your drawing is made up from..!
The last point is how to save all layers of the PictureBox:
void saveImage(string filename)
{
using (Bitmap bmp = new Bitmap(pictureBox1.ClientSize.Width,
pictureBox1.ClientSize.Height))
{
pictureBox1.DrawToBitmap(bmp, pictureBox1.ClientRectangle);
bmp.Save("yourFileName.png", ImageFormat.Png);
}
}
I'm trying to make a little 15x15 playing grid, and drawing the solid outline rectangles side-by-side is working just fine, however when I try to call FillRectangle() to fill the aforementioned cells the results are .. Not quite what I expected.. I think it's easier if I just show you..
Here's the code
//Draw grid now since InitGrid() is done
using (Graphics formGraphics = this.CreateGraphics())
{
Pen pen = new Pen(Color.Black);
SolidBrush brush = new SolidBrush(Color.Beige);
int y = new int();
List<Rectangle> rect = new List<Rectangle>();
foreach (Cell cell in pGrid.cells)
{
rect.Add(new Rectangle(cell.Point.X, cell.Point.Y, 18, 18));
formGraphics.DrawRectangle(pen, rect[y]);
formGraphics.FillRectangle(brush, rect[y++]);
}
}
Also an extra question, since I am using a using code block do I still have to call Dispose() on the Pen and Graphics?
The differences in the "covered area" are caused by DrawRectangle using a Pen which has a thickness of 1px, and 0.5px of that is added to the outer edges of the rectangle, and then rounded "to the bottom-right". These bottom-right borders are then overwritten by the next cell's FillRectangle.
You could use a border thickness of 2, which would cause the border to be 2 pixels larger than the filled area, and thus appear 1px thick on each side.
Pen pen = new Pen(Color.Black, 2);
This only works if there is actual space between the cells to show the borders in. If there is no room for the borders you'll have to either shrink your cell size or grow the grid itself.
Another solution (since you mention the outlines are fine) is to first call FillRectangle and then DrawRectangle.
Also a using around the pen and brush won't hurt. Note that you can also use the static Pens.Black (unless you're going for the solution of a 2px thick border) and Brushes.Beige.
I need to create some simple graphics in a windows form using C#. By simple I mean lines, circles etc. However, when I draw e.g. a filled circle, the edge is not smooth (as expected when drawing a circle using square pixels), but when drawing the same circle with the same number of pixels in a vector program it looks perfect. I have been drawing in Inkscape for this example.
Maybe the vector software uses some sort of render function to smooth the colors, but is this possible in C# without creating too much code? Here is an example of the code, which is using Graphics to create a canvas to draw on.
private void StatGraphicsPanel_Paint(object sender, PaintEventArgs e)
{
Graphics canvas = e.Graphics;
Brush brush = Brushes.Aqua;
canvas.FillEllipse(brush, 0, 0, 10, 10);
}
Solution
This code does the trick:
private void StatGraphicsPanel_Paint(object sender, PaintEventArgs e)
{
Graphics canvas = e.Graphics;
canvas.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
Brush brush = Brushes.Aqua;
canvas.FillEllipse(brush, 0, 0, 10, 10);
}
You want to use the System.Drawing.Graphics.SmoothingMode property. Before beginning to use your Graphics object, do this:
canvas.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
That should give you a nice anti-aliased ellipse instead of the default jaggy one.
Similarly, if you want to draw high-quality text in an image, use the System.Drawing.Graphics.TextRenderingHint property.
As others have mentioned in comments, you might want to consider using WPF instead. WinForms are dated.
I have a picturebox that has an image in it and a top of this image I am drawing some ellipses. However, only some of the ellipses show up. Code looks something like this:
Graphics g = Graphics.FromHwnd(pictureBox1.Handle);
g.FillEllipse(redBrush, rfidNode1.readerPos.X, rfidNode1.readerPos.Y, 15, 15);
EDIT: I'm sorry I copied and pasted the last line twice...so there is only one line that fills the ellipse. Also, x and y are within the range of the picture box.
Could you try something like this? (change the dimensions if needed)
Bitmap bmp = new Bitmap(pictureBox1.Width, pictureBox1.Height);
Graphics g = Graphics.FromImage(bmp);
g.FillEllipse(redBrush, rfidNode1.readerPos.X, rfidNode1.readerPos.Y, 15, 15);
pictureBox1.Image = bmp;
Or maybe I missed your intentions?
If the X and Y are same, you are drawing two ellipses one on top another, so only the last is visible. Also, it could be that the X and Y are out of bounds of picture box ?
try to override the paint event and place there your painting code. drawing processes run very often, and then your graphic just gets overdrawn.
Tutorial - Drawing with C#
For drawing on a control, try to register with the paint-event and use the graphics object provided in the paint event args.
Look here for details and an example.
I' m not very sure if it really possible in secure way draw over picture box. Secure I mean: to be sure that all your ellipses are visible when you want. If you want some custom behaviuor, PictureBox is not so good solution, by me.
Like a solution I would suggest to draw an image manually in place where now you have picture box.
Hope this helps.
Regards.
I have drawn a circle in windows form
Pen pen = new Pen(Color.Black, 3);
Graphics gr = this.CreateGraphics();
gr.DrawEllipse(pen, 5,5,20,20);
How to delete it...
You have to clear your Graphic:
Graphics.Clear();
But all drawn figures will be cleared. Simply, you will then need to redraw all figures except that circle.
Also, you can use the Invalidate method:
Control.Invalidate()
It indicates a region to be redrawn inside your Graphics. But if you have intersecting figures you will have to redraw the figures you want visible inside the region except the circle.
This can become messy, you may want to check out how to design a control graph or use any graph layout library.
You can invalidate the draw region you want to refresh for example:
this.Invalidate();
on the form...
Assuming you're subscribing to the Paint event or overriding the protected OnPaint routine, then you will need to perform something like this:
bool paint = false;
protected override void OnPaint(object sender, PaintEventArgs e)
{
if (paint)
{
// Draw circle.
}
}
Then when you want to stop painting a circle:
paint = false;
this.Invalidate(); // Forces a redraw
You can make a figure of same dimensions using the backColor of your control in which you are drawing
use after your code to clear your figure.
Pen p = new Pen(this.BackColor);
gr.DrawEllipse(p, 5,5,20,20);
In fact, you can delete your circle and nothing but your circle.
Everything you need is something like a screenshot of the "before state" of the area you want to clear, to make a TextureBrush from it. You can achieve that step by something like this:
Bitmap _Background = new Bitmap(this.Width, this.Height);
Graphics.FromImage(_Background).CopyFromScreen(this.Left, this.Top, 0, 0, this.Size);
The first line will give you a bitmap in your windows forms size. The second line will save a screenshot of it in the _Background-bitmap.
Now you create a TextureBrush out of it:
Brush brsBackground = new TextureBrush(_Background);
The next thing you need are the dimensions of your circle, so you should save them into a variable, if they are not a fix value. When you got them at hand, you can clear the specific area like this:
Graphics gr = this.CreateGraphics();
gr.FillEllipse(brsBackground, 5, 5, 20, 20); // values referred to your example
Done!
Even complex figures are able to be deleted by this, like a GraphicsPath for example:
GraphicsPath gp = new GraphicsPath(); // any kind of GraphicsPath
gr.FillRegion(brsBackground, new Region(gp));
You don't "delete" it per se, there's nothing to delete. It's a drawing, you draw something else over it or you can call the Graphics.Clear() method.
If u are using Invalidate() and is not working, make a panel.Refresh().
That will work on you.
just make another control with the attributes etc. that you want, make the visibility to false and set the region of the control to the other control like this:
pen.Region = pen2.Region;
It is very simple to delete a drawn circle from c.
There is only four steps:-
Open turbo app
go to the command where you had drawn the circle
drag the command
click on delete button