How to draw text with transparency value with C#? - c#

I create a from with transparent background color and I want to draw "hello" string on the form window with a proper transparency setting:
private void Form1_Paint(object sender, PaintEventArgs e)
{
Bitmap bmp = new Bitmap(150, 50);
Graphics g = Graphics.FromImage(bmp);
g.Clear(Color.Transparent);
g.SmoothingMode = SmoothingMode.AntiAlias;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.CompositingQuality = CompositingQuality.GammaCorrected;
g.CompositingMode = CompositingMode.SourceOver;
g.TextRenderingHint = TextRenderingHint.SingleBitPerPixel;
g.TextContrast = 10;
Font font = new Font("", 20, FontStyle.Bold);
Color color = ColorTranslator.FromHtml("#D3D3D3");
int opacity = 180;
SolidBrush brush = new SolidBrush(Color.FromArgb(opacity, color));
g.DrawString("hello", font, brush, 10, 10);
g.Save();
g.Dispose();
bmp.MakeTransparent(Color.Transparent);
e.Graphics.DrawImage(bmp, 0, 0);
bmp.Dispose();
}
When I set opacity to 1 or 2, the image text disappears, when set to 3, display dark black color, when set to 254, a little transparent. Anything wrong in my code?

I update code to:
private void Form1_Paint(object sender, PaintEventArgs e)
{
Bitmap bmp = new Bitmap(200, 200);
Graphics g = Graphics.FromImage(bmp);
g.Clear(Color.Black);
g.SmoothingMode = SmoothingMode.HighQuality;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.CompositingQuality = CompositingQuality.GammaCorrected;
g.CompositingMode = CompositingMode.SourceOver;
g.TextRenderingHint = TextRenderingHint.SingleBitPerPixel;
g.TextContrast = 3;
Font font = new Font("", 20, FontStyle.Bold);
Color color = ColorTranslator.FromHtml("#191970");
int opacity = 20;
SolidBrush brush = new SolidBrush(Color.FromArgb(opacity, color));
g.DrawString("hello", font, brush, 20, 20);
bmp.MakeTransparent(Color.Black);
g.Save();
g.Dispose();
e.Graphics.DrawImage(bmp, 0, 0);
bmp.Dispose();
}
Output on screen:

Related

why the created bmp image hasn't got high quality?

I have searched a lot and used almost all preferred methods but i have not got the result of having a high quality bitmap image: the below is the code i have used:
ean13.Scale = 0.8f;
ean13.Scale = (float)Convert.ToDecimal(cboScale.Items[cboScale.SelectedIndex]);// it includes the sizes
Bitmap bmp = ean13.CreateBitmap();
Graphics g = Graphics.FromImage(bmp);
g.DrawString(tcc.Text, this.Font, Brushes.Black, 120, 80);// title
g.DrawString(bcc.Text, this.Font, Brushes.Black, 120, 1); //the price
g.SmoothingMode = SmoothingMode.AntiAlias;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
g.TextRenderingHint = TextRenderingHint.SingleBitPerPixelGridFit;
g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.PixelOffsetMode = PixelOffsetMode.HighQuality;
g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
g.TextRenderingHint = TextRenderingHint.SingleBitPerPixelGridFit;
g.Flush();
this.picbox.Image = bmp;
added the picture:
added the expected picture
You should configure the graphics quality before drawing the text. Currently all these changes have no effect on the bitmap.

picturebox image text print is not clear

private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
Font font = new Font("Arial", 8, FontStyle.Regular);
//e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
//e.Graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
e.Graphics.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
// e.SmoothingMode = SmoothingMode.AntiAlias;
// e.InterpolationMode = InterpolationMode.HighQualityBicubic;
//e.PixelOffsetMode = PixelOffsetMode.HighQuality;
e.Graphics.DrawString("FIRST NAME" , myFont, Brushes.Black, 15, 90);
e.Graphics.DrawString("MIDDLE NAME", font, Brushes.Black, 15, 105);
e.Graphics.DrawString("LASTNAME", font, Brushes.Black, 15, 120);
e.Graphics.DrawString("ADDRESS", font, Brushes.Black, 15, 135);
e.Graphics.Flush();
}
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
float x = 0;
float y = 0;
//SolidBrush myBrush = new SolidBrush(Color.Black);
e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
e.Graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
e.Graphics.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
Bitmap bmp = new Bitmap(this.pictureBox1.Width, this.pictureBox1.Height);
this.pictureBox1.DrawToBitmap(bmp, new Rectangle(0, 0, this.pictureBox1.Width, this.pictureBox1.Height));
e.Graphics.DrawImage((Image)bmp, x, y);
}
print font word are not clear visible when i print picturebox image.
i want clear font (like microsoft word or excel print) letters visible when i printing images.
means good pixels
anybody suggest me?

Font is very ugly

I'm drawing a string with the folowing code:
public Image DrawString(String lString)
{
Image lImage = new Bitmap(128, 128);
Rectangle rec = new Rectangle(0, 0, lImage.Width, lImage.Height);
Graphics g = Graphics.FromImage(lImage);
g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SingleBitPerPixelGridFit;
drawFormat = new StringFormat();
drawFormat.Alignment = StringAlignment.Center;
drawFormat.LineAlignment = StringAlignment.Center;
Font font = new Font("Arial", 20, FontStyle.Regular);
font = FindBestFitFont(g, lString, font, rec.Size);
g.DrawString(lString, font, Brushes.Red, rec, drawFormat);
return lImage;
}
The font looks very ugly even when i use:
g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SingleBitPerPixelGridFit;
Is there a way to make the font more smooth?
Try
g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
instead.
Have you tried AntiAliasing or ClearType?
g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;

visual c# - onPaint and transparency

I am making a simple form with two semi-transparent texts
and i put it in a paint event.
only, when I wider the form, the texts turn darker and grainy.
actualy I want the darker color but not the grainy effect.
here is my code snippet:
private void sbfToolBox_Paint(object sender, PaintEventArgs e)
{
System.Drawing.Graphics formGraphics = this.CreateGraphics();
formGraphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
string drawString = "tekst";
System.Drawing.Font drawFont = new System.Drawing.Font("Arial", 50);
Color color_red = Color.FromArgb(30, 100, 0, 0);
Color color_cyan = Color.FromArgb(30, 0, 100, 100);
System.Drawing.SolidBrush brush_red = new System.Drawing.SolidBrush(color_red);
System.Drawing.SolidBrush brush_cyan = new System.Drawing.SolidBrush(color_cyan);
float x = 0.0F;
float x2 = 20.0F;
float y = 50.0F;
formGraphics.DrawString(drawString, drawFont, brush_red, x, y);
formGraphics.DrawString(drawString, drawFont, brush_cyan, x2, y);
drawFont.Dispose();
brush_red.Dispose();
brush_cyan.Dispose();
formGraphics.Dispose();
}
thanks in advance
Use the Graphics object from PaintEventArgs.
Change
System.Drawing.Graphics formGraphics = this.CreateGraphics();
To
System.Drawing.Graphics formGraphics = e.Graphics;
And remove
formGraphics.Dispose();

Generate transparent PNG c#

I have the function below to generate a sample logo. What I want to do is to return a transparent png or gif instead of a white background.
How can I do that?
private Bitmap CreateLogo(string subdomain)
{
Bitmap objBmpImage = new Bitmap(1, 1);
int intWidth = 0;
int intHeight = 0;
Font objFont = new Font(
"Arial",
13,
System.Drawing.FontStyle.Bold,
System.Drawing.GraphicsUnit.Pixel);
Graphics objGraphics = Graphics.FromImage(objBmpImage);
intWidth = (int)objGraphics.MeasureString(subdomain, objFont).Width;
intHeight = (int)objGraphics.MeasureString(subdomain, objFont).Height;
objBmpImage = new Bitmap(objBmpImage, new Size(intWidth, intHeight));
objGraphics = Graphics.FromImage(objBmpImage);
objGraphics.Clear(Color.White);
objGraphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
objGraphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
objGraphics.DrawString(
subdomain, objFont,
new SolidBrush(Color.FromArgb(102, 102, 102)), 0, 0);
objGraphics.Flush();
return (objBmpImage);
}
Here is the end result:
context.Response.ContentType = "image/png";
using (MemoryStream memStream = new MemoryStream())
{
CreateLogo(_subdname).Save(memStream, ImageFormat.Png);
memStream.WriteTo(context.Response.OutputStream);
}
In the CreateLogo function:
objGraphics.Clear(Color.White) was changed to objGraphics.Clear(Color.Transparent)
new SolidBrush(Color.FromArgb(102, 102, 102)) changed to new SolidBrush(Color.FromArgb(255, 255, 255))
You can do something like this:
Bitmap bmp = new Bitmap(300, 300);
Graphics g = Graphics.FromImage(bmp);
g.Clear(Color.Transparent);
g.FillRectangle(Brushes.Red, 100, 100, 100, 100);
g.Flush();
bmp.Save("test.png", System.Drawing.Imaging.ImageFormat.Png);
Take a look at Can you make an alpha transparent PNG with C#?

Categories