I have two pictureboxes: One in the background with an image in it (picturebox1) and another one,(pic1) were i want to paint something (this background should be transparent.--> pic1.BackColor = Color.Transparent;)
It look like that:
Everything works great except the font. why does it have a black border?
My code looks like that:
private void InBitmapZeichnen()
{
Graphics g1 = Graphics.FromImage(bmp12);
g1.PageUnit = GraphicsUnit.Pixel;
//g1.InterpolationMode = InterpolationMode.HighQualityBilinear;
Font f = new Font("Verdana", 8f);
Font f1 = new Font("Verdana", 8f);
Font f2 = new Font("Verdana", 10, System.Drawing.FontStyle.Bold);
Brush b = new SolidBrush(Color.YellowGreen);
Brush b1 = new SolidBrush(Color.YellowGreen);
Pen PenRaster = new Pen(Color.Black, 0.1f);
if (mnuRaster.Checked == true)
{
float j = Rohrdurchmesser / (float)(trk.Value + 2);
//g1.SmoothingMode = SmoothingMode.HighSpeed;
for (int i = pic1.Width / (trk.Value + 2); i <= pic1.Width - pic1.Width / (trk.Value + 2); i += pic1.Width / (trk.Value + 2))
{
PointF PRaster1 = new PointF(i, 0);
PointF PRaster2 = new PointF(i, pic1.Bottom);
PointF PRaster3 = new PointF(0, i+4);
PointF PRaster4 = new PointF(pic1.Right, i+4);
g1.DrawString((j).ToString("0") + " mm", f, b, new PointF(i + 5, 5));
g1.DrawString((j).ToString("0") + " mm", f, b, new PointF(5, i + 5));
g1.DrawLine(PenRaster, PRaster1, PRaster2);
g1.DrawLine(PenRaster, PRaster3, PRaster4);
j += Rohrdurchmesser / (float)(trk.Value + 2);
}
}
}
When I select a color for backcolor it works fine:
Try use GraphicsPath if you want to draw text on trasparent background:
private void InBitmapZeichnen()
{
Graphics g1 = Graphics.FromImage(bmp12);
g1.PageUnit = GraphicsUnit.Pixel;
g1.SmoothingMode = SmoothingMode.AntiAlias;
//g1.InterpolationMode = InterpolationMode.HighQualityBilinear;
Font f = new Font("Verdana", 8f);
Font f1 = new Font("Verdana", 8f);
Font f2 = new Font("Verdana", 10, System.Drawing.FontStyle.Bold);
Brush b = new SolidBrush(Color.YellowGreen);
Brush b1 = new SolidBrush(Color.YellowGreen);
Pen PenRaster = new Pen(Color.Black, 0.1f);
if (mnuRaster.Checked == true)
{
float j = Rohrdurchmesser / (float)(trk.Value + 2);
//g1.SmoothingMode = SmoothingMode.HighSpeed;
for (int i = pic1.Width / (trk.Value + 2); i <= pic1.Width - pic1.Width / (trk.Value + 2); i += pic1.Width / (trk.Value + 2))
{
PointF PRaster1 = new PointF(i, 0);
PointF PRaster2 = new PointF(i, pic1.Bottom);
PointF PRaster3 = new PointF(0, i + 4);
PointF PRaster4 = new PointF(pic1.Right, i + 4);
using (var path = new GraphicsPath())
{
path.AddString((j).ToString("0") + " mm", f.FontFamily, (int)f.Style, f.Size, new Point(i + 5, 5), null);
path.AddString((j).ToString("0") + " mm", f.FontFamily, (int)f.Style, f.Size, new Point(5, i + 5), null);
g1.FillPath(b, path);
}
//g1.DrawString((j).ToString("0") + " mm", f, b, new PointF(i + 5, 5));
//g1.DrawString((j).ToString("0") + " mm", f, b, new PointF(5, i + 5));
g1.DrawLine(PenRaster, PRaster1, PRaster2);
g1.DrawLine(PenRaster, PRaster3, PRaster4);
j += Rohrdurchmesser / (float)(trk.Value + 2);
}
}
}
As you can see instead of DrawString i used FillPath.
I think need to clear Back Color before you start drawing on picture box.
Graphics g1 = Graphics.FromImage(bmp12);
// add clear
g1.Clear(BackColor);
So try this statement in your code...!!!
Related
I've been drawn multiple rectangles on the picturebox image.I want to resize all that rectangles using mouse events. Can anyone help me out regarding this?
public List<rectangle> listRec = new List<rectangle>();
Graphics g;
//private Graphics g;
Point startPos;
Point currentPos;
bool drawing;
Rectangle r1;
Rectangle rect = new Rectangle();
private Rectangle getRectangle()
{
r1 = new Rectangle(
Math.Min(startPos.X, currentPos.X),
Math.Min(startPos.Y, currentPos.Y),
Math.Abs(startPos.X - currentPos.X),
Math.Abs(startPos.Y - currentPos.Y));
return r1;
}
private void button1_Click(object sender, EventArgs e)
{
String data;
Font font = new Font("Arial", 14);
arg1 = Convert.ToInt32(textBox1.Text);
arg2 = Convert.ToInt32(textBox2.Text);
Rectangle rect = new Rectangle();
rect.Size = new Size(40, 65);
for (int x = 0; x < arg1; x++)
{
// rect.X = x * rect.Width;
rect.X = x * (rect.Width + 30) + 73;
for (int y = 0; y < arg2; y++)
{
rect.Y = y * (rect.Height + 35) + 38;
listRec.Add(rect);
data = rect.ToString();
TextWriter txt = new StreamWriter("E:\\B1Pockets.txt", true);
txt.WriteLine(data);
txt.Close();
// MessageBox.Show(rect.ToString());
}
}
foreach (Rectangle rec in listRec)
{
g = pictureBox1.CreateGraphics();
Pen p = new Pen(Color.Red, 3);
g.DrawRectangle(p, rec);
g.DrawString("p1", font, new SolidBrush(Color.Yellow), (rect.Width + 30), 35);
g.DrawString("p2", font, new SolidBrush(Color.Yellow), (rect.Width + 40) + 60, 35);
g.DrawString("p3", font, new SolidBrush(Color.Yellow), (rect.Width + 40) + 130, 35);
g.DrawString("p4", font, new SolidBrush(Color.Yellow), (rect.Width + 30), (rect.Height + 30) + 40);
g.DrawString("p5", font, new SolidBrush(Color.Yellow), (rect.Width + 40) + 60, (rect.Height + 30) + 40);
g.DrawString("p6", font, new SolidBrush(Color.Yellow), (rect.Width + 40) + 130, (rect.Height + 30) + 40);
}
}
I've tried this code to my application.I drawn rectangles in 2*3 manner.and also draw a big rectangle above it.In short my picturebox containing many rectangles and i want to add resizing options for all the rectangles in c#
The question is simple, assuming you have a shape (lets say a rectangle) and a text ("hello" for example), so it'll write the text all accross the borders of the rectangle for as many times as it fits, for example:
hello hello hello hello
hello hello
hello hello
hello hello hello hello
In order to do it I assume you'd need to use a graphics variable, I just dont know how to do it.
A code for drawing a string in a bitmap object:
Bitmap tempp = new Bitmap(1, 1);
Graphics g = Graphics.FromImage(tempp);
SizeF w = g.MeasureString("22", new Font("Tahoma", 200));//in order to get the size of the string as a pixel measurement
Bitmap bmp = new Bitmap((int)w.Width+1, (int)w.Height+1);//the bitmap that will contain the text as a picture
RectangleF rectf = new RectangleF(0, 0, (int)w.Width+1, (int)w.Height+1);
g = Graphics.FromImage(bmp);
g.SmoothingMode = SmoothingMode.AntiAlias;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.PixelOffsetMode = PixelOffsetMode.HighQuality;
g.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;
StringFormat format = new StringFormat()
{
Alignment = StringAlignment.Center,
LineAlignment = StringAlignment.Center
};
g.DrawString("22", new Font("Tahoma", 200), Brushes.Black, rectf, format);
g.Flush();
Thanks in advance.
For second comment:
hello hello hello
hel llo
hel llo
hello hello hello
I hope this is what you need. There isn't much to explain here. The logic is pretty straightforward.
public string MyString = "Hello"
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
var g = e.Graphics;
var strFont = new Font("Tahoma", 10);
var strSizeF = g.MeasureString(MyString, strFont);
var canvas = new Rectangle
{
Height = 200,
Width = 200,
Location = new Point(10, 10),
};
g.DrawRectangle(new Pen(new SolidBrush(Color.Blue)), canvas);
var nx = (int)(canvas.Width / strSizeF.Width);
var ny = (int)(canvas.Height / strSizeF.Height);
var spacingX = (canvas.Width - nx * strSizeF.Width) / (nx-1);
var spacingY = (canvas.Height - ny * strSizeF.Height) / (ny-1);
//draw top row and bottom row
int i;
for (i = 0; i < nx; i++)
{
g.DrawString(
MyString,
strFont,
Brushes.Black,
new PointF(canvas.X + i*(strSizeF.Width + spacingX), canvas.Y)
);
g.DrawString(
MyString,
strFont,
Brushes.Black,
new PointF(canvas.X + i * (strSizeF.Width + spacingX), canvas.Y + canvas.Height - strSizeF.Height)
);
}
//divide the string into half
var isLengthOdd = MyString.Length % 2 != 0;
var substr1 = MyString.Substring(0, MyString.Length / 2 + (isLengthOdd ? 1 : 0));
var substr2 = MyString.Substring(MyString.Length / 2, MyString.Length - MyString.Length / 2);
var substr2SizeF = g.MeasureString(substr2, strFont);
//draw side rows
for (i = 1; i < ny - 1; i++)
{
g.DrawString(
substr1,
strFont,
Brushes.Black,
new PointF(canvas.X, canvas.Y + i * (strSizeF.Height + spacingY))
);
g.DrawString(
substr2,
strFont,
Brushes.Black,
new PointF(canvas.X + canvas.Width - substr2SizeF.Width, canvas.Y + i * (strSizeF.Height + spacingY))
);
}
}
Result:
I'm currently working on Tracking Object by Color on my WebCam, and i have it going so far but i want to add an option to draw multiple Objects. Until now it only Draws a Rectangle around the biggest Object.
BlobCounter blobcounter = new BlobCounter();
blobcounter.MinHeight = 100;
blobcounter.MinWidth = 100;
blobcounter.ObjectsOrder = ObjectsOrder.Size;
blobcounter.ProcessImage(grayImage);
Rectangle[] rects = blobcounter.GetObjectsRectangles();
if (checkBox1.Checked == false)
{
if (rects.Length > 0)
{
Rectangle objectRect1 = rects[0];
Graphics g = Graphics.FromImage(video);
using (Pen pen = new Pen(Color.LightGreen, 3))
{
g.DrawRectangle(pen, objectRect1);
PointF drawPoin = new PointF(objectRect1.X, objectRect1.Y);
int objectX = objectRect1.X + objectRect1.Width / 2 - video.Width / 2;
int objectY = video.Height / 2 - (objectRect1.Y + objectRect1.Height / 2);
PointF drawPoin2 = new PointF(objectRect1.X, objectRect1.Y + objectRect1.Height + 4);
String Blobinformation = "X= " + objectX.ToString() + " Y= " + objectY.ToString() + "\nSize=" + objectRect1.Width + ", " + objectRect1.Height;
g.DrawString(Blobinformation, new Font("Arial", 12), new SolidBrush(Color.LightSkyBlue), drawPoin2);
}
g.Dispose();
}
}
else
{
??????????
}
Adding a simple foreach loop should suffice. I don't know how efficient the drawing is, but I'm almost certain it wont be a problem with a few rectangles.
else
{
if (rects.Length > 0)
{
foreach (Rectangle objectRect in rects)
{
Graphics g = Graphics.FromImage(video);
using (Pen pen = new Pen(Color.LightGreen, 3))
{
g.DrawRectangle(pen, objectRect);
PointF drawPoin = new PointF(objectRect.X, objectRect.Y);
int objectX = objectRect.X + objectRect.Width / 2 - video.Width / 2;
int objectY = video.Height / 2 - (objectRect.Y + objectRect.Height / 2);
PointF drawPoin2 = new PointF(objectRect.X, objectRect.Y + objectRect.Height + 4);
String Blobinformation = "X= " + objectX.ToString() + " Y= " + objectY.ToString() + "\nSize=" + objectRect.Width + ", " + objectRect.Height;
g.DrawString(Blobinformation, new Font("Arial", 12), new SolidBrush(Color.LightSkyBlue), drawPoin2);
}
g.Dispose();
}
}
}
I am having trouble drawing graphics on a simple form. My code compiles and runs, but the intended red box does not show up.
I am using the full .NET Framework 4 in Visual Studio 2010.
What is going wrong, and how can it be fixed?
private void Form1_Load(object sender, EventArgs e)
{
System.Drawing.Graphics graphicsObj;
graphicsObj = this.CreateGraphics();
Pen myPen = new Pen(System.Drawing.Color.Red, 5);
Rectangle myRectangle = new Rectangle(20, 20, 250, 200);
graphicsObj.DrawRectangle(myPen, myRectangle);
}
EDIT: Working But Slow Code
void BatteryPaint(object sender, EventArgs e)
{
TabPage page = (TabPage)sender;
if (!controlsSetup)
{
populateBatteryTab(page);
controlsSetup = true;
}
//Create the items
Rectangle busBar = new Rectangle();
Rectangle batPack = new Rectangle();
Rectangle pack1Outline = new Rectangle();
Rectangle pack2Outline = new Rectangle();
Rectangle pack3Outline = new Rectangle();
Rectangle pack4Outline = new Rectangle();
Color GreenZone = Color.FromArgb(150, 0, 255, 0);
Color YellowZone = Color.FromArgb(150, 255, 255, 0);
Color RedZone = Color.FromArgb(150, 255, 0, 0);
Color greyZone = Color.FromArgb(200, 200, 200, 200);
Graphics graphicControl = page.CreateGraphics();
SolidBrush busBarBrush = new SolidBrush(Color.Peru);
SolidBrush GreenBrush = new SolidBrush(GreenZone);
SolidBrush GreyBrush = new SolidBrush(greyZone);
Pen packPen = new Pen(Color.LightGray, (float)8);
Point busBarTop = new Point(page.Width / 64, page.Height / 32);
Point busBarBottom = new Point(busBarTop.X, busBarTop.Y + page.Height / 6);
//change the properties
//Bus Bar Top
busBar.Width = page.Width*153 / 640;
busBar.Height = page.Height / 64;
busBar.Location = busBarTop;
graphicControl.FillRectangle(busBarBrush, busBar);
//Bus Bar Bottom
busBar.Location = busBarBottom;
graphicControl.FillRectangle(busBarBrush, busBar);
//Pack 1
batPack.Width = page.Width / 20;
batPack.Height = (busBarBottom.Y + busBar.Height) - busBarTop.Y;
batPack.Location = new Point(busBarTop.X + page.Width / packSpacingMultiplier, busBarTop.Y);
pack1Outline.Width = batOutlineWidth;
graphicControl.FillRectangle(GreenBrush, batPack);
pack1Outline.Height = (3 * (Battery.Width + page.Width / batSpacingMultiplier) + page.Width / batSpacingMultiplier);
pack1Outline.Location = new Point(BatPack1.X - (page.Width / batSpacingMultiplier), BatPack1.Y - (page.Width / batSpacingMultiplier));
for(int numBats = 0; numBats < 30; numBats++)
{
Battery.Location = new Point(BatPack1.X + ((numBats % 10) * (Battery.Width + page.Width / batSpacingMultiplier)), BatPack1.Y + ((numBats / 10) * (Battery.Width + page.Width / batSpacingMultiplier)));
graphicControl.FillEllipse(new SolidBrush(BatteryZone(0.00)), Battery);
}
//Pack 2
batPack.Location = new Point(batPack.Location.X + batPack.Width + page.Width / packSpacingMultiplier, batPack.Location.Y);
graphicControl.FillRectangle(GreenBrush, batPack);
pack2Outline.Width = batOutlineWidth;
pack2Outline.Height = (3 * (Battery.Width + page.Width / batSpacingMultiplier) + page.Width / batSpacingMultiplier);
pack2Outline.Location = new Point(BatPack2.X - (page.Width / batSpacingMultiplier), BatPack2.Y - (page.Width / batSpacingMultiplier));
for(int numBats = 0; numBats < 30; numBats++)
{
Battery.Location = new Point(BatPack2.X + ((numBats % 10) * (Battery.Width + page.Width / batSpacingMultiplier)), BatPack2.Y + ((numBats / 10) * (Battery.Width + page.Width / batSpacingMultiplier)));
graphicControl.FillEllipse(new SolidBrush(BatteryZone(0.00)), Battery);
}
//Pack 3
batPack.Location = new Point(batPack.Location.X + batPack.Width + page.Width / packSpacingMultiplier, batPack.Location.Y);
graphicControl.FillRectangle(GreenBrush, batPack);
pack3Outline.Width = batOutlineWidth;
pack3Outline.Height = (3 * (Battery.Width + page.Width / batSpacingMultiplier) + page.Width / batSpacingMultiplier);
pack3Outline.Location = new Point(BatPack3.X - (page.Width / batSpacingMultiplier), BatPack3.Y - (page.Width / batSpacingMultiplier));
for(int numBats = 0; numBats < 30; numBats++)
{
Battery.Location = new Point(BatPack3.X + ((numBats % 10) * (Battery.Width + page.Width / batSpacingMultiplier)), BatPack3.Y + ((numBats / 10) * (Battery.Width + page.Width / batSpacingMultiplier)));
graphicControl.FillEllipse(new SolidBrush(BatteryZone(0.00)), Battery);
}
//Pack 4
batPack.Location = new Point(batPack.Location.X + batPack.Width + page.Width / packSpacingMultiplier, batPack.Location.Y);
graphicControl.FillRectangle(GreyBrush, batPack);
pack4Outline.Width = batOutlineWidth;
pack4Outline.Height = (3 * (Battery.Width + page.Width / batSpacingMultiplier) + page.Width / batSpacingMultiplier);
pack4Outline.Location = new Point(BatPack4.X - (page.Width / batSpacingMultiplier), BatPack4.Y - (page.Width / batSpacingMultiplier));
for(int numBats = 0; numBats < 30; numBats++)
{
Battery.Location = new Point(BatPack4.X + ((numBats % 10) * (Battery.Width + page.Width / batSpacingMultiplier)), BatPack4.Y + ((numBats / 10) * (Battery.Width + page.Width / batSpacingMultiplier)));
graphicControl.FillEllipse(new SolidBrush(BatteryZone(0.00)), Battery);
}
//add the controls
graphicControl.DrawRectangle(packPen, pack1Outline);
graphicControl.DrawRectangle(packPen, pack2Outline);
graphicControl.DrawRectangle(packPen, pack3Outline);
graphicControl.DrawRectangle(packPen, pack4Outline);
}
You shouldn't do your drawing in the Load event, which is fired before your form is even displayed for the first time. The window is repainted when it is displayed, which clears the graphics you've drawn. If you want a persistent rectangle, handle the Paint event instead:
private void Form1_Paint(object sender, PaintEventArgs e)
{
System.Drawing.Graphics graphicsObj = e.Graphics;
Pen myPen = new Pen(System.Drawing.Color.Red, 5);
Rectangle myRectangle = new Rectangle(20, 20, 250, 200);
graphicsObj.DrawRectangle(myPen, myRectangle);
}
If you don't want a persistent rectangle (highly doubtful, but still possible), try handling the Shown event instead of the Load event.
If the graphics displayed will be updated frequently (e.g. in a game), make sure to set DoubleBuffered to true and override OnPaint instead of handling an event:
protected override void OnPaint(PaintEventArgs e)
{
Graphics g = e.Graphics;
Pen myPen = new Pen(Color.Red, 5);
g.DrawRectangle(myPen, 20, 20, 250, 200);
}
i am using TabControl in my windows form application (c#)
and
i want draw the Highlight over the top of the tabpage headers using hightlightcolor.
http://img4up.com/up2/83871411772596923665.jpg
thanks
I hope this will help you. Just apply this below mentioned code in OnDrawItem Event of Tab Control
if (e.Index == SelectedIndex)
{
Rectangle rect = new Rectangle(e.Bounds.X + 4, e.Bounds.Y, e.Bounds.Width - 6, e.Bounds.Height);
backColorBrush = new SolidBrush(Color.CornflowerBlue);
e.Graphics.FillRectangle(backColorBrush, rect);
string tabName = this.TabPages[e.Index].Text;
TabPages[e.Index].BackColor = Color.AliceBlue;
TabPages[e.Index].BorderStyle = BorderStyle.None;
TabPages[e.Index].UseVisualStyleBackColor = false;
TabPages[e.Index].RightToLeft = RightToLeft.No;
myFormat.Alignment = StringAlignment.Near;
myFont = new Font(e.Font, FontStyle.Bold);
RectangleF r1 = new RectangleF(e.Bounds.X + 1, e.Bounds.Y + 4, e.Bounds.Width, e.Bounds.Height - 4);
foreColorBrush = new System.Drawing.SolidBrush(Color.Black);
e.Graphics.DrawString(tabName, myFont, foreColorBrush, r1, myFormat);
// e.Graphics.DrawImage(img, new Point(rect.X + (GetTabRect(e.Index).Width - _imageLocation.X), _imageLocation.Y));
}
else
{
myFont = new Font(e.Font, FontStyle.Bold);
backColorBrush = new System.Drawing.SolidBrush(Color.CadetBlue);
foreColorBrush = new System.Drawing.SolidBrush(Color.Black);
TabPages[e.Index].BackColor = Color.AliceBlue;
string tabName = TabPages[e.Index].Text;
Rectangle rect = new Rectangle(e.Bounds.X + 1, e.Bounds.Y, e.Bounds.Width - 1, e.Bounds.Height + 1);
myFormat.Alignment = StringAlignment.Near;
e.Graphics.FillRectangle(backColorBrush, rect);
RectangleF r1 = new RectangleF(e.Bounds.X, e.Bounds.Y + 4, e.Bounds.Width, e.Bounds.Height - 4);
e.Graphics.DrawString(tabName, myFont, foreColorBrush, r1, myFormat);
//e.Graphics.DrawImage(img, new Point(rect.X + (GetTabRect(e.Index).Width - _imageLocation.X), _imageLocation.Y));
}
myFormat.Dispose();
myFont.Dispose();
backColorBrush.Dispose();
foreColorBrush.Dispose();