C# GDI Drawing2D help - c#

What GDI methods can I use to draw the blue shape shown in the image below? The center must be transparent.

There are a number of ways but you'll probably want to use the following:
FillRectangle
FillPolygon
DrawLine
since it looks like your shape can be reduced to a rectangle and two polygons and then outlined by a few lines.
Here is a really simple and hard-coded example of what i was thinking:
Private Sub Control_Paint(ByVal sender As Object, ByVal e As PaintEventArgs) _
Handles MyBase.Paint
Dim g As Graphics = e.Graphics
g.FillRectangle(Brushes.Aqua, New Rectangle(10, 10, 10, 90))
g.FillPolygon(Brushes.Aqua, New Point() { _
New Point(10, 10), _
New Point(20, 10), _
New Point(40, 50), _
New Point(30, 50)})
g.FillPolygon(Brushes.Aqua, New Point() { _
New Point(10, 100), _
New Point(20, 100), _
New Point(40, 50), _
New Point(30, 50)})
g.DrawLine(Pens.Black, New Point(10, 10), New Point(10, 100))
g.DrawLine(Pens.Black, New Point(10, 100), New Point(20, 100))
g.DrawLine(Pens.Black, New Point(20, 100), New Point(40, 50))
g.DrawLine(Pens.Black, New Point(40, 50), New Point(20, 10))
g.DrawLine(Pens.Black, New Point(20, 10), New Point(10, 10))
...

Im assuming GDI+ here aka System.Drawing namespace.
The best thing to do is to look at System.Drawing.Drawing2d.GraphicsPath class :
http://msdn.microsoft.com/en-us/library/system.drawing.drawing2d.graphicspath.aspx
You need to make sure you close the path to get the hollow effect.

Wouldn't it just be easier to draw it using a bitmap? That's what they're for anyway :).

Related

Display barcode number below barcode image [duplicate]

I have never worked with drawing before and im having a little issue. I cant seem to get the output of this code to work.
The file is saving but it is not drawing on the text. Can anyone see what i may have done wrong?
EDIT: A silly mistake - the backgrond of the image was white (and the brush colour was!). The text is not centered however as i would have expected. Any ideas why SO? :)
EDIT: Image is below.
Thanks
Bitmap myBitmap = new Bitmap(#"C:\Users\Scott\desktop\blank.bmp");
Graphics g = Graphics.FromImage(myBitmap);
g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
g.DrawString("My\nText",
new Font("Tahoma", 20),
Brushes.White,
new PointF(0, 0));
StringFormat strFormat = new StringFormat();
strFormat.Alignment = StringAlignment.Center;
strFormat.LineAlignment = StringAlignment.Center;
g.DrawString("My\nText",
new Font("Tahoma", 20), Brushes.White,
new RectangleF(0, 0, 500, 500),
strFormat);
myBitmap.Save(#"C:\Users\Scott\desktop\blank1.bmp");
I am sure you might be looking for this.
rectf = new RectangleF(655, 460, 535, 90); //rectf for My Text
using(Graphics g = Graphics.FromImage(myBitmap))
{
//g.DrawRectangle(new Pen(Color.Red, 2), 655, 460, 535, 90);
g.SmoothingMode = SmoothingMode.AntiAlias;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.PixelOffsetMode = PixelOffsetMode.HighQuality;
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Center;
sf.LineAlignment = StringAlignment.Center;
g.DrawString("My\nText", new System.Drawing.Font("Tahoma", 32, FontStyle.Bold), Brushes.Black, rectf, sf);
}
//g.DrawRectangle(new Pen(Color.Red, 2), 655, 460, 535, 90); Line is used to show where your text will be written. So before you actually make your make your text You can see where this rectanlge will be created on the image. If you want the center of the image you can find the height and width and divide that by 2 to find the center of the image and than can plot the rectangle parameters accordingly.

The same output for all graphics objects

I have several outputs for my datas and want to create one fill-Object for any output-possibility. For example PrintDocument and Bitmap. Thats works with Graphics-Object, but the result is different.
My test method for fill graphics:
protected void fillGraphics(Graphics g)
{
g.PageUnit = GraphicsUnit.Pixel;
Pen pen = new Pen(Brushes.Black, 3);
g.DrawRectangle(pen, 100, 100, 300, 300);
g.DrawString("Wichtig!", new System.Drawing.Font(FontFamily.GenericSansSerif, 16), new SolidBrush(Color.Black), 120, 120);
}

C# fill polygon (triangle)

I have problem with draw two polygons.
I want to fill two triangles, but one is greater than the second.
I am using UserControl in winforms.
Code:
Point[] DOWN = new Point[] {new Point(0, 0), new Point(10, 0), new Point(5, 5)};
Point[] UP = new Point[] { new Point(0, 15), new Point(10, 15), new Point(5, 10) };
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
SolidBrush brush = new SolidBrush(Color.FromArgb(253, 198, 19));
e.Graphics.FillPolygon(brush, DOWN);
e.Graphics.FillPolygon(brush, UP);
brush.Dispose();
}
Where is problem?
Try setting the PixelOffsetMode property:
e.Graphics.PixelOffsetMode = PixelOffsetMode.Half;
using (SolidBrush brush = new SolidBrush(Color.FromArgb(253, 198, 19))) {
e.Graphics.FillPolygon(brush, DOWN);
e.Graphics.FillPolygon(brush, UP);
}
Result:
Try keeping the order counter-clockwise and start from the highest point:
new Point(5, 10), new Point(10, 15), new Point(0, 15)
Tell us if that helped. Sometimes those algorithms don't behave well on border conditions.

Interpolation anomaly during drawing

I have a problem with AntiAliasing smoothing mode and drawing.
Let say I have a signal with min and max values at the same points.
So I want to display it to see where it "thicker".
So the method I use is to draw vertical lines and use antialiasing.
Here is the problem, the rising edge seems to be antialiased, but the falling not.
If I added some noise to the second signal the same thing observable.
Without noise
With noise
![With noise][2]
Can anyone point out what am I missing? Or this problem comes from somewhere else?
Code (moved from comments):
Bitmap drawBitmap = new Bitmap(pictureBox1.Height, _
pictureBox1.Width, _
System.Drawing.Imaging.PixelFormat.Format32bppArgb);
Graphics drawGraph;
Point[] pts = new Point[] { new Point(0, 60), new Point(0, 59), new Point(1, 35), _
new Point(1, 47), new Point(2, 25), new Point(2, 35), _
new Point(3, 17), new Point(3, 25), new Point(4, 12), _
new Point(4, 27), new Point(5, 10), new Point(5, 22), _
new Point(6, 10), new Point(6, 11), new Point(7, 11), _
new Point(7, 16), new Point(8, 16), new Point(8, 24), _
new Point(9, 24), new Point(9, 34), new Point(10, 34), _
new Point(10, 46), new Point(11, 46), new Point(11, 59), _
new Point(12, 59), new Point(12, 72)};
using (drawGraph = Graphics.FromImage(drawBitmap)) {
drawGraph.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic;
drawGraph.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias;
for (int i = 1; i < pts.Length - 1; i += 2) {
drawGraph.DrawLine(new Pen(Color.Black, 1), pts[i], pts[i - 1]);
drawGraph.DrawLine(new Pen(Color.Black, 1), pts[i], pts[i + 1]);
}
}
pictureBox1.Image = drawBitmap;
Apply a pixel offset mode as well:
drawGraph.PixelOffsetMode = Drawing2D.PixelOffsetMode.HighQuality;
The InterpolationMode can be removed as it do nothing with lines (only with images when resized).

Draw border around bitmap

I have got a System.Drawing.Bitmap in my code.
The width is fix, the height varies.
What I want to do, is to add a white border around the bitmap, with about 20 pixel, to all 4 edges.
How would this work?
You could draw a rectangle behind the bitmap. The width of the rectangle would be (Bitmap.Width + BorderWidth * 2), and the position would be (Bitmap.Position - new Point(BorderWidth, BorderWidth)). Or at least that's the way I'd go about it.
EDIT:
Here is some actual source code explaining how to implement it (if you were to have a dedicated method to draw an image):
private void DrawBitmapWithBorder(Bitmap bmp, Point pos, Graphics g) {
const int borderSize = 20;
using (Brush border = new SolidBrush(Color.White /* Change it to whichever color you want. */)) {
g.FillRectangle(border, pos.X - borderSize, pos.Y - borderSize,
bmp.Width + borderSize, bmp.Height + borderSize);
}
g.DrawImage(bmp, pos);
}
You can use 'SetPixel' method of a Bitmap class, to set nesessary pixels with the color. But more convenient is to use 'Graphics' class, as shown below:
bmp = new Bitmap(FileName);
//bmp = new Bitmap(bmp, new System.Drawing.Size(40, 40));
System.Drawing.Graphics gr = System.Drawing.Graphics.FromImage(bmp);
gr.DrawLine(new Pen(Brushes.White, 20), new Point(0, 0), new Point(0, 40));
gr.DrawLine(new Pen(Brushes.White, 20), new Point(0, 0), new Point(40, 0));
gr.DrawLine(new Pen(Brushes.White, 20), new Point(0, 40), new Point(40, 40));
gr.DrawLine(new Pen(Brushes.White, 20), new Point(40, 0), new Point(40, 40));
Below function will add border around the bitmap image. Original image will increase in size by the width of border.
private static Bitmap DrawBitmapWithBorder(Bitmap bmp, int borderSize = 10)
{
int newWidth = bmp.Width + (borderSize * 2);
int newHeight = bmp.Height + (borderSize * 2);
Image newImage = new Bitmap(newWidth, newHeight);
using (Graphics gfx = Graphics.FromImage(newImage))
{
using (Brush border = new SolidBrush(Color.White))
{
gfx.FillRectangle(border, 0, 0,
newWidth, newHeight);
}
gfx.DrawImage(bmp, new Rectangle(borderSize, borderSize, bmp.Width, bmp.Height));
}
return (Bitmap)newImage;
}

Categories