I used an advise from this topic: Rounded edges in picturebox C# to make my picturebox edges rounded. So, I claimed a new control using this code:
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
class OvalPictureBox : PictureBox {
public OvalPictureBox() {
this.BackColor = Color.DarkGray;
}
protected override void OnResize(EventArgs e) {
base.OnResize(e);
using (var gp = new GraphicsPath()) {
gp.AddEllipse(new Rectangle(0, 0, this.Width-1, this.Height-1));
this.Region = new Region(gp);
}
}
}
But there is my question: how could I add antialias to my control? I read some topics like this: Possible to have anti-aliasing when drawing a clipped image? but there is just drawing functions are used, and I need to implement antialias in my own control, which parent is PictureBox.
I also tried to override OnPaint method to get PaintEventArgs to use the SmoothingMode, but my code sucked and didn`t work properly.
So I deleted it and this is what I have now:
protected override void OnPaint(PaintEventArgs pe)
{
base.OnPaint(pe);
using (var gp = new GraphicsPath())
{
gp.AddEllipse(new Rectangle(0, 0, this.Width - 1, this.Height - 1));
this.Region = new Region(gp);
}
}
What should I add to this method?
Related
I used this answer: Alpha in ForeColor
to create a custom label element that allowed fading through ARGB unlike the default label.
using System;
using System.Drawing;
using System.Windows.Forms;
public class MyLabel : Label {
protected override void OnPaint(PaintEventArgs e) {
Rectangle rc = this.ClientRectangle;
StringFormat fmt = new StringFormat(StringFormat.GenericTypographic);
using (var br = new SolidBrush(this.ForeColor)) {
e.Graphics.DrawString(this.Text, this.Font, br, rc, fmt);
}
}
}
I was curious how I would implement TextAlign into this class allowing the text contents to be aligned correctly.
Thanks to #Aybe's comment, I worked out I needed to add the Alignment to the StringFormat var fmt like this:
fmt.Alignment = StringAlignment.Center;
Making the entire class look like this:
using System;
using System.Drawing;
using System.Windows.Forms;
public class MyLabel : Label {
protected override void OnPaint(PaintEventArgs e) {
Rectangle rc = this.ClientRectangle;
StringFormat fmt = new StringFormat(StringFormat.GenericTypographic);
fmt.Alignment = StringAlignment.Center;
using (var br = new SolidBrush(this.ForeColor))
{
e.Graphics.DrawString(this.Text, this.Font, br, rc, fmt);
}
}
}
I have a ToolStripContainer on a winform. I want to set the BackColor of its ContentPanel to custom gradient color.
How can I set it?
You can create a simple class that inherits from ToolStripContainer and set up BackgroundImage (or anything else) in the its constructor. For example:
class MyToolStripContainer : System.Windows.Forms.ToolStripContainer
{
public MyToolStripContainer()
{
var rect = new Rectangle(0, 0, 300, 300);
using (Bitmap bitmap = new Bitmap(rect.Height, rect.Width))
using (Graphics graphics = Graphics.FromImage(bitmap))
using (LinearGradientBrush brush = new LinearGradientBrush(
rect,
Color.Blue,
Color.Red,
LinearGradientMode.ForwardDiagonal))
{
brush.SetSigmaBellShape(0.5f);
graphics.FillRectangle(brush, rect);
ContentPanel.BackgroundImage = Image.FromHbitmap(bitmap.GetHbitmap());
}
ContentPanel.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
}
}
Not only ToolStripContentPanel, no winform controls supports "Gradient colors" out of the box, but they provide a way to custom painting. That's how we can accomplish the gradient look and feel in winforms.
toolStripContainer1.ContentPanel.Paint += ContentPanel_Paint;//Triggering up the paint event
public void ContentPanel_Paint(object sender, PaintEventArgs e)
{
ToolStripContentPanel panel = (ToolStripContentPanel)sender;
using (var brush = new LinearGradientBrush(panel.ClientRectangle, Color.Gray, Color.Red, 90))
{
e.Graphics.FillRectangle(brush, panel.ClientRectangle);
}
}
The problem isn't that I don't know how to make a border-less form re-sizable, or to how to draw a border. The problem is what happens when you re-size the form with that custom border.
Here is a screenshot, because I don't know how to explain it:
Here is how I created the border (currently):
private void Form1_Paint(object sender, PaintEventArgs e)
{
int width = 1;
Rectangle rec = this.ClientRectangle;
ButtonBorderStyle bbs = ButtonBorderStyle.Solid;
Color clr = Color.Gray;
ControlPaint.DrawBorder(e.Graphics, rec, clr, width, bbs, clr, width, bbs, clr, width, bbs, clr, width, bbs);
}
As for re-sizing a border-less form; I created a repository for the project.
Resize Custom Border - Bitbucket
I don't have any idea as to why this happens, so I wouldn't know where to begin. I just need to draw a border without it doing this. I have tried other ways of drawing one, but the results were the same.
Hopefully this and the repository becomes useful for anyone trying to do the same.
Thank you for taking your time to read if you did.
Try to use Graphics.DrawRectangle instead of DrawBorder
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
Single fWidth = 5.0f;
Rectangle r = new Rectangle(0,0,this.ClientRectangle.Width-1,this.ClientRectangle.Height-1);
e.Graphics.DrawRectangle(new Pen(Color.Gray, fWidth), r);
}
protected override void OnResize(EventArgs e)
{
base.OnResize(e);
this.Invalidate();
}
Use Graphic library :
Step 1: Override the OnPaint handler for your main form
Step 2: Define a rectangle that covers your current form
Step 3: Draw the defined rectangle
protected override void OnPaint(PaintEventArgs e)
{
Rectangle r = new Rectangle(0,0,this.ClientRectangle.Width-1,this.ClientRectangle.Height-1);
e.Graphics.DrawRectangle(new Pen(Color.Gray, 1.0f), r);
}
You may also implement this using a condition statement like:
this.form.Resize += // some handler1
//in hadler1
{
this.form.Paint += // Your new paint handler2
}
//in handler2
{
Rectangle r = new Rectangle(0,0,this.ClientRectangle.Width-1,this.ClientRectangle.Height-1);
e.Graphics.DrawRectangle(new Pen(Color.Gray, 1.0f), r);
}
I am new to Dot Net, I want to print a rectangle with 20mm width and 8mm height exactly if I measure with scale. I also want to print text exactly in the middle of rectangle.Can anyone suggest me how can I achieve this?
I am really sorry for not being clear earlier. I have tried using "PageUnits" its working fine. However, I have problem with the margins.
I am able to print correct margins(8.8mm left and 22mm Top) if I am using the printer "HP LaserJet P2035n". If print using "Canon iR2020 PCL5e" I am getting incorrect margins(8.1mm left and 8.0mm Top) where I should get 8.8mm left and 22mm top margins. Can someone explain me where I am doing wrong.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Printing;
namespace ConsoleApplication6
{
class DrawShape
{
public static void DrawRec()
{
PrintDocument doc = new PrintDocument();
doc.PrintPage += doc_PrintPage;
doc.Print();
}
static void doc_PrintPage(object sender, PrintPageEventArgs e)
{
Graphics g = e.Graphics;
PageSettings PageSet = new PageSettings();
float MarginX = PageSet.PrintableArea.X;
float MarginY = PageSet.PrintableArea.Y;
float x = (float)(8.8-((MarginX/100)*25.4));
float y = (float)(22-((MarginY/100)*25.4));
g.PageUnit = GraphicsUnit.Millimeter;
g.DrawRectangle(Pens.Black, x, y, 20, 8);
}
}
}
You may want to start with this:
private void button1_Click(object sender, EventArgs e)
{
using (Graphics formGraphics = this.CreateGraphics())
{
formGraphics.PageUnit = GraphicsUnit.Millimeter;
formGraphics.DrawRectangle(Pens.Blue, 0, 0, 20, 80);
}
}
You can then using DrawString on the Graphics object to draw text inside your rectangle.
I am using a control paint event to draw graphic objects in my application. The sizes of all the object is stored with size unit of millimeters and therefore i use 'millimeter' as PageUnit for the graphic object. For some reason when i draw a shape with a DashStyle other than solid, it gets drawn in a very unexpected scale.
In the code example below I expect to see both lines being drawn one on top of the other, but what i get is the red dashed line being drawn elsewhere in larger scale.
Any idea what I am missing?
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication6
{
public partial class Form1 : Form
{
private Pen solidBlackPen = new Pen(Color.Black, 1);
private Pen dashedRedPen = new Pen(Color.Red, 1) {
DashStyle = DashStyle.Dash
};
private Point point1 = new Point(5, 5);
private Point point2 = new Point(35, 5);
public Form1()
{
InitializeComponent();
this.BackColor = Color.White;
this.Paint += new PaintEventHandler(Form1_Paint);
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
e.Graphics.PageUnit = GraphicsUnit.Millimeter;
e.Graphics.DrawLine(solidBlackPen, point1, point2);
e.Graphics.DrawLine(dashedRedPen, point1, point2);
}
}
}
Since I am new i cant upload a screenshot.
After few tests this looks to me like some kind of bug that occures mybe only on specific Os/framework.
What managed to fix me this was adding the following line before drawing the shapes:
e.Graphics.ScaleTransform(1, 1);