Graphics.DrawString specify opacity of text - c#

Is it possible to specify the opacity of text written using the Graphics.DrawString method?
I'm doing something like this, but would like my text to be semi-transparent if it is possible.
Currently I'm doing this:
Graphics graphics = Graphics.FromImage(image);
graphics.DrawString("This is a watermark",
new Font("Arial", 40),
new SolidBrush(Color.Red),
0,
0);

Try:
int opacity = 128; // 50% opaque (0 = invisible, 255 = fully opaque)
Graphics graphics = Graphics.FromImage(image);
graphics.DrawString("This is a watermark",
new Font("Arial", 40),
new SolidBrush(Color.FromArgb(opacity, Color.Red)),
0,
0);

Try
new SolidBrush(Color.FromArgb(0x78FF0000))
Hope this helps

Related

Adding two image into one image c#?

I want to combine two image but I cant success . Help me please.
When try the combine PictureBox show only first image but there is not second image , when I remove first image I can see second image.
Also I tried setting first image and draw the text on image that's also not working. Please help.
Image myimg = Code128Rendering.MakeBarcodeImage(textBox1.Text, 2, true);
Bitmap image = new Bitmap(myimg.Width + 20, myimg.Height + 50);
pictureBox1.DrawToBitmap(image, new Rectangle(0, 0, myimg.Width + 20, myimg.Height + 50));
Bitmap bmp = new Bitmap(myimg.Width + 20, myimg.Height);
Bitmap bmp2 = new Bitmap(myimg.Width + 20, 20);
Graphics Cizgi2 = Graphics.FromImage(bmp2);
Graphics Cizgi = Graphics.FromImage(bmp);
Cizgi.DrawImage(myimg, 0, 0);
FontStyle sitil = FontStyle.Bold;
Font fonts = new Font(new FontFamily("Arial"), 10, sitil);
Cizgi2.DrawString(textBox1.Text, fonts, Brushes.Black, 5, myimg.Height + 10);
Graphics g = Graphics.FromImage(image);
g.DrawImage(bmp, new Point(10, 0));
g.DrawImage(bmp2, new Point(0, bmp.Height + 10));
I want to image seems like first but i cant make
It looks like you are trying to concatenate two images vertically? It's pretty simple actually, you can look here (C# image concatenation), but I've also modified it for your needs. I think this should work:
float drawBorderX = 5;
float drawBorderY = 5;
//Set up our two images
Bitmap barCode = Code128Rendering.MakeBarcodeImage(textBox1.Text, 2, true);
Bitmap text = new Bitmap(barCode.Width, 50);
Graphics textGraphics = Graphics.FromImage(text);
//Draw the text to the bottom image.
FontStyle sitil = FontStyle.Bold;
Font fonts = new Font(new FontFamily("Arial"), 10, sitil);
textGraphics.FillRectangle(new SolidBrush(Color.White), new Rectangle(0, 0, text.Width, text.Height));
textGraphics.DrawString(textBox1.Text, fonts, Brushes.Black, drawBorderX, drawBorderY);
//Vertically concatenate the two images.
Bitmap resultImage = new Bitmap(Math.Max(barCode.Width, text.Width), barCode.Height + text.Height);
Graphics g = Graphics.FromImage(resultImage);
g.DrawImage(barCode, 0, 0);
g.DrawImage(text, 0, barCode.Height);
Edit: Note that resultImage will contain the image you want, so you can set your PictureBox to be that image at the end.

Overlay bitmap on another bitmap

On a single bitmap I need to display graphs and text values. So what I did is create a bitmap with points and creating a another bitmap with the text and place on the large bitmap.
I tried using the brush to write the text, but I am not able to see the underlying graphics even though trasparency is set.
Instead I thought to set the transparency for the text bitmap, but the bitmap which I have created are 24 bit rgb. So can we set the transparency for the 24 bit map.
Bitmap textBitmap = null;
textBitmap = new Bitmap(10, 10, PixelFormat.Format24bppRgb);
using (Graphics memoryGrahics =
Graphics.FromImage(textBitmap))
{
memoryGrahics.FillRectangle(Brushes.Black, new Rectangle(0, 0, 100, 100));
memoryGrahics.DrawString(result, f, Brushes.White, x, y);
}
//placing the text bitmap on the graphbitmap
using (Graphics g = Graphics.FromImage(GraphBitmap))
{
g.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceOver;
textBitmap.MakeTransparent();
g.DrawImage(textBitmap, 0, 0);
return GraphBitmap;
}
well.. it seems like you are using 2 different Graphical objects... although 1 Graphics objects with 1 bitmap can handle multiple layouts of custom drawings, like so:
int width = 800, height = 600;
var bit = new Bitmap(width, height);
var g = Graphics.FromImage(bit);
g.SmoothingMode = SmoothingMode.AntiAlias;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
var area = new Rectangle(0, 0, width, height);
g.FillRectangle(new LinearGradientBrush(area, Color.PaleGoldenrod, Color.OrangeRed, 45), area);
g.DrawImage(Image.FromFile(#"your image"), new Point(10, 10));
g.DrawString("sample", new System.Drawing.Font("Tahoma", 56), new SolidBrush(Color.Black), new PointF(50, 50));
pictureBox1.Image = bit;
note that g.DrawImage method can be used to load other bitmaps as well

How can I use DrawString clearly?

I need to draw a string with Graphics class with DrawString method. But image has some black pixels, not the exact color. How can I draw it clearly?
public Image Ciz()
{
Color renk = Color.FromArgb(255, 133, 199);
Bitmap result = new Bitmap(KutuBoyutu.Genislik, KutuBoyutu.Yukseklik);
result.SetResolution(100, 100);
Graphics g = Graphics.FromImage(result);
g.SmoothingMode = SmoothingMode.HighQuality;
Brush drawBrush = new SolidBrush(renk);
StringFormat stringFormat = new StringFormat();
stringFormat.Alignment = StringAlignment.Center;
stringFormat.LineAlignment = StringAlignment.Center;
Rectangle rect1 = new Rectangle(0, 0, result.Width, result.Height);
Font font = new Font("Arial", 15f, FontStyle.Bold, GraphicsUnit.Pixel);
g.DrawString(Yazi, font, drawBrush, rect1, stringFormat);
drawBrush.Dispose();
return result;
}
Drawing text requires a well-defined background so that the anti-aliasing effect can work properly. You don't have any, you forgot to initialize the bitmap. Which left its pixels at the default, black with an alpha of 0.
So the text renderer will try to alias the letter to blend into a black background. You see that, those almost-black pixels now become very visible against a white background. It will only look good if you draw the bitmap on top of a black background. Fix:
using (Graphics g = Graphics.FromImage(result)) {
g.Clear(Color.White);
// etc...
}
If you cannot fix the background color then you need to give up on anti-aliasing. Set the TextRenderingHint to SingleBitPerPixelGridFit. You won't like it much :)
Add
g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
to your code.
TextRenderingHint.AntiAliasGridFit works as well.

How to scale text when drawing to an Image with c#

I would like to draw some text in a rectangle and have it scale to the maximum size that fits within the rectangle.
So far I have this:
Bitmap bitmapImage = new Bitmap(500, 500);
Graphics graphicImage = Graphics.FromImage(bitmapImage);
graphicImage.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
var rect = new Rectangle(0, 0, 500, 500);
graphicImage.DrawString( "testing testing 123!", new Font("Arial", 12, FontStyle.Bold), Brushes.Black, rect);
bitmapImage.Save("test.png");
it draws the text but doesn't scale up the font size.
Call Graphics.MeasureString in a binary search loop.

How to draw centered text onto a jpg using system.drawing in c#

I am using the following code to draw text onto a jpg image but it requires x/y coordinate percision on where to place the text.
var bmp = new Bitmap("C:\\testing\\Given.jpg");
var gra = Graphics.FromImage(bmp);
var text = "The Berman's";
var font = new Font("Segoe Script", 24);
var brush = Brushes.Orange;
var point = new PointF(130, 224);
gra.DrawString(text, font, brush, point);
bmp.Save("C:\\testing\\Custom.jpg");
How would I go about centering text onto an image? I am guessing it would have to do with defining some sort of container (rectangle maybe?) that is the width of the image and centering the text within that? Not sure what the best practice would be for this.
using(var sf = new StringFormat()
{
Alignment = StringAlignment.Center,
LineAlignment = StringAlignment.Center,
})
{
gra.DrawString(text, font, brush, new Rectangle(0, 0, bmp.Width, bmp.Height), sf);
}

Categories