I need to convert text into an image. Does anyone know if I can use the creative version of imageresizer.net to do this?
I have searched a lot but haven't found anything to confirm or deny imageresizer can do this.
Why use imagerezier when you can do it with c# out-of-box? From https://stackoverflow.com/a/2070493/1224069:
private Image DrawText(String text, Font font, Color textColor, Color backColor)
{
//first, create a dummy bitmap just to get a graphics object
Image img = new Bitmap(1, 1);
Graphics drawing = Graphics.FromImage(img);
//measure the string to see how big the image needs to be
SizeF textSize = drawing.MeasureString(text, font);
//free up the dummy image and old graphics object
img.Dispose();
drawing.Dispose();
//create a new image of the right size
img = new Bitmap((int) textSize.Width, (int)textSize.Height);
drawing = Graphics.FromImage(img);
//paint the background
drawing.Clear(backColor);
//create a brush for the text
Brush textBrush = new SolidBrush(textColor);
drawing.DrawString(text, font, textBrush, 0, 0);
drawing.Save();
textBrush.Dispose();
drawing.Dispose();
return img;
}
Related
I tried this code, it print distorted image and text on black background of a desktop.
I am using a Form with a transparent background. I want to print image on the background.
this.TransparencyKey = BackColor;
Bitmap original = null;
original = new Bitmap(imagePath);
Bitmap newImage = new Bitmap(width, height + rect.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
var newGrp = Graphics.FromImage(newImage);
newGrp.Clear(Color.Transparent);
newGrp.InterpolationMode = InterpolationMode.HighQualityBilinear;
newGrp.SmoothingMode = SmoothingMode.HighQuality;
newGrp.TextRenderingHint = TextRenderingHint.AntiAlias;
newGrp.CompositingQuality = CompositingQuality.HighQuality;
newGrp.DrawImage(original, 0, rect.Height, width, height);
newGrp.DrawString(text, font, color, textStartPosition, 0);
this.BackgroundImage = (Image)newImage;
Original is bitmap from image file and color of text is gray.The form is transparent. The new image is painted as background of form. Depend on background color of desktop(dynamic color), I want to print image on form, But my concerns about text is not clear.
I expect clear image and text on black background.
Generate an Image From Text
public Image DrawText(String text, Font font, Color textColor, Color backColor)
{
//first, create a dummy bitmap just to get a graphics object
Image img = new Bitmap(1, 1);
Graphics drawing = Graphics.FromImage(img);
//measure the string to see how big the image needs to be
SizeF textSize = drawing.MeasureString(text, font);
//free up the dummy image and old graphics object
img.Dispose();
drawing.Dispose();
//create a new image of the right size
img = new Bitmap((int)textSize.Width, (int)textSize.Height);
drawing = Graphics.FromImage(img);
//paint the background
drawing.Clear(backColor);
//create a brush for the text
Brush textBrush = new SolidBrush(textColor);
drawing.DrawString(text, font, textBrush, 0, 0);
drawing.Save();
textBrush.Dispose();
drawing.Dispose();
return img;
}
And Callback
Font font = new Font("Arial", 20, FontStyle.Bold);
Color color = Color.FromArgb(255, 0, 0); //Red
Color background = Color.Transparent; //None Color
Image img = DrawText("Hoàng Long", font, color, background);
img.Save(context.Response.OutputStream, ImageFormat.Jpeg);
Tried:How to generate an image from text on fly at runtime
Please help me
Disclaimer: this code will create a white background. Not an Transparent. To make it Transparent you need to change this
var image = DrawTextImage(TextForImage, font, Color.Black, Color.Transparent);
public class TextToImage
{
// main method you need to call.
public byte[] GetImageFromText(string TextForImage)
{
FontFamily fontFamily = new FontFamily("Arial");
Font font = new Font(fontFamily, 40.0f);
var image = DrawTextImage(TextForImage, font, Color.Black, Color.Transparent);
var imgData = ImageToByteArray(image);
return imgData;
}
// convert image to byte array
private byte[] ImageToByteArray(System.Drawing.Image imageIn)
{
MemoryStream ms = new MemoryStream();
imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
return ms.ToArray();
}
private Image DrawTextImage(String text, Font font, Color textColor, Color backColor)
{
return DrawTextImage(text, font, textColor, backColor, new Size(500, 500));
}
// create image
private Image DrawTextImage(String text, Font font, Color textColor, Color backColor, Size minSize)
{
//first, create a dummy bitmap just to get a graphics object
SizeF textSize;
using (Image img = new Bitmap(1, 1))
{
using (Graphics drawing = Graphics.FromImage(img))
{
//measure the string to see how big the image needs to be
textSize = drawing.MeasureString(text, font);
if (!minSize.IsEmpty)
{
textSize.Width = textSize.Width > minSize.Width ? textSize.Width : minSize.Width;
textSize.Height = textSize.Height > minSize.Height ? textSize.Height : minSize.Height;
}
}
}
//create a new image of the right size
//Image retImg = new Bitmap((int)textSize.Width, (int)textSize.Height);
Image retImg = new Bitmap(500, 500);
using (var drawing = Graphics.FromImage(retImg))
{
//paint the background
drawing.Clear(backColor);
//create a brush for the text
using (Brush textBrush = new SolidBrush(textColor))
{
drawing.TextRenderingHint = TextRenderingHint.AntiAlias;
RectangleF rectF1 = new RectangleF(10, 10, 490, 490);
drawing.DrawString(text, font, textBrush, rectF1);
drawing.Save();
}
}
return retImg;
}
}
I think above code is self explanatory. you just create an instance of this helper class and pass text param.
TextToImage textToImage = new TextToImage();
byte[] byteArr = textToImage.GetImageFromText("some text to create image out of it");
Hope this helps. :)
How can the font size and the position of the watermark text in an image based on image size set?
I'm looking for a solution to alter the font size according to the image size.
also Transparent watermark.
my function:
public void fn_TextWaterMark(string text,ImageFormat fmt)
{
string sourceImage = "c:\\a.jpg;
string targetImage = "c:\\a-watermark.jpg;
try
{
//open source image as stream and create a memorystream for output
FileStream source = new FileStream(sourceImage, FileMode.Open);
Stream output = new MemoryStream();
Image img = Image.FromStream(source);
//choose font for text
Font font = new Font("Times New Roman", 20, FontStyle.Bold, GraphicsUnit.Pixel);
//choose color and transparency
Color color = Color.FromArgb(255, 255, 255, 255);
//location of the watermark text in the parent image
default//Point pt = new Point(100, 100);
Point pt = new Point(img.Width*31/100, img.Height*46/100);
SolidBrush brush = new SolidBrush(color);
//draw text on image
Graphics graphics = Graphics.FromImage(img);
graphics.DrawString(text, font, brush, pt);
graphics.Dispose();
//update image memorystream
img.Save(output, fmt);
Image imgFinal = Image.FromStream(output);
//write modified image to file
Bitmap bmp = new System.Drawing.Bitmap(img.Width,img.Height, img.PixelFormat);
Graphics graphics2 = Graphics.FromImage(bmp);
graphics2.DrawImage(imgFinal, new Point(0,0));
bmp.Save(targetImage, fmt);
imgFinal.Dispose();
img.Dispose();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
pic examp:
http://i.stack.imgur.com/ZShUI.jpg
http://i.stack.imgur.com/GUfbM.jpg
thanks
I have made a Windows Form application and I want to generate a delivery note with a barcode. I have embed the barcode font, but I get an error. See this question: Embed Barcode in C# PDF Library
Now, I want to make an image from the barcode and embed this image on my delivery note. I have searched on Google for doing this, and I found the following code:
private Image DrawBarcodeAfleverbonImage(String text)
{
Font barcodeFont = new Font("Bar-Code 39", 31, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point);
//Font barcodeFont = new Font("Arial", 31, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point);
//first, create a dummy bitmap just to get a graphics object
Image img = new Bitmap(1, 1);
Graphics drawing = Graphics.FromImage(img);
//measure the string to see how big the image needs to be
SizeF textSize = drawing.MeasureString(text, barcodeFont);
//free up the dummy image and old graphics object
img.Dispose();
drawing.Dispose();
//create a new image of the right size
img = new Bitmap((int)textSize.Width, (int)textSize.Height);
drawing = Graphics.FromImage(img);
//create a brush for the text
Brush textBrush = new SolidBrush(Color.Black);
drawing.DrawString(text, barcodeFont, textBrush, 0, 0);
drawing.Save();
img.Save(#"C:\Users\Marten\Documents\test.png");
textBrush.Dispose();
drawing.Dispose();
return img;
If I run my program an image will be created. There is just one problem: the barcode font is too thick, so I can not scan:
What is wrong?
You need to set a background color before drawing your text:
drawing.Clear(Color.White);
drawing.DrawString(text, barcodeFont, textBrush, 0, 0);
Or if you want a transparent background you need to turn off font smoothing.
drawing.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SingleBitPerPixel;
drawing.DrawString(text, barcodeFont, textBrush, 0, 0);
I have a PictureBox with lots of transparent png's drawn into...
Now I'd like to save the content of this PictureBox to a file, but without the transparency.
How can I do this?
I have already tried to remove transparency from the Image like this, but it didn't work, I still got a transparent image after the save.
...
removeTransparency(pictureBox.Image).Save(saveFileDialog.FileName);
private Bitmap removeTransparency(Image transparentImage)
{
Bitmap src = new Bitmap(transparentImage);
Bitmap newImage = new Bitmap(src.Size.Width, src.Size.Height);
Graphics g = Graphics.FromImage(newImage);
g.DrawRectangle(new Pen(new SolidBrush(Color.White)), 0, 0, newImage.Width, newImage.Height);
g.DrawImage(src, 0, 0);
return newImage;
}
You may cycle through all pixel (please not using GetPixel/SetPixel) to change the color or you may create another bitmap with the same size, create a graphics context from the image, clear the image with your favorite background color and then draw your image on it (so transparent pixels simply will be replaced by the background color).
Example
Did you do something like this?
public static Bitmap Repaint(Bitmap source, Color backgroundColor)
{
Bitmap result = new Bitmap(source.Width, source.Height, PixelFormat.Format32bppArgb);
using (Graphics g = Graphics.FromImage(result))
{
g.Clear(backgroundColor);
g.DrawImage(source, new Rectangle(0, 0, source.Width, source.Height));
}
return result;
}