I am writing info entered on a web form into a tiff file. My issue is where the comment box comes into play for the web form. The comment box is multi-line and when writing it on to the tiff file, some of the information entered into the comment box falls out of the tiff image.
The code for how I am trying to write it to the tiff file:-
System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(800, 1000); //Creates Bitmap
Graphics g = Graphics.FromImage(bitmap);
g.DrawString("Comment: " + CommentBox.Text, outputFont, Brushes.Black, new PointF(0, 700)); // Writing the text from the comment box on to the Tiff file.
so what that means to me is, if I am writing a multi line comment as:-
"Hello Testing.
Hello Testing again and again and again and again and again and again and again.
Hello Testing again and again and again and again and again and again and again. Hello Testing again and again and again and again and again and again and again."
My tiff-file captures line elements only upto the 1000 width, the elements beyond that width do not automatically get generated in a new line.
Can anyone help me with this? ideas?
Try something like this - you can use the overload of DrawString that takes a containing rectangle, and the text should wrap within that:
System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(800, 1000); //Creates Bitmap
using(Graphics g = Graphics.FromImage(bitmap))
{
RectangleF rect = new RectangleF(new PointF(0, 700), new SizeF(200,200)); // adjust these accordingly for your bounding rect
StringFormat drawFormat = new StringFormat();
drawFormat.Alignment = StringAlignment.Near;
g.DrawString("Comment: " + CommentBox.Text, outputFont, Brushes.Black, rect, drawFormat); // Writing the text from the comment box on to the Tiff file.
}
Related
I am writing a program that is going to dump an SVG file, in C#. I was wondering if there is any way of measuring how big a piece of text is going to be from the C# program.
I can assume that I know the font and fontsize. But the "average size of letter"*number_of_letters is very inaccurate.
I am looking for some option like this:
1) Just figure out, from font size and font, how big the text is.
2) I could dump the SVG once, and get the measure from there (by rendering it somehow? and then reading that information from it, somehow?)
Thanks!
Figured it out:
Font stringFont = new Font("Verdana", 8, GraphicsUnit.Pixel);
Image newImage = new Bitmap(10, 10);
Graphics g = Graphics.FromImage(newImage);
SizeF stringSize = new SizeF();
g.PageUnit = GraphicsUnit.Pixel;
stringSize = g.MeasureString("Hello, this is \n a string and stuff", stringFont);
I'm able to scan a JPG image using Tesseract, I'm able to scan a regular PDF using ITextSharp and get the text from those. But I can't find a way to either get the text from a scanned PDF with a .PDF extension, or convert a PDF to an image so I can then scan it with Tesseract. Are there any options that I'm missing? Thanks!
Assuming that you have scanned the PDF document. Secondly assuming you have only text in the PDF document. You can generate an image from text from the following method
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;
}
Reference: How to generate an image from text on fly at runtime
I'm developing an Winforms application to be used for Led displays. The Led display is size 64x32. I created a test bitmap image with 4 cells size 64x16, so I can output it it to the LED display.
When the user enters 4 numbers in the input form, those Strings are then converted to Bitmap Image and placed inside the cells. I somehow got it to work with this method; It edits the same bitmap image, inserts the numbers, and saves it into a new bmp (or am I wrong?).
public static Bitmap Convert_Text_to_Image(string txt, string fontname, int fontsize)
{
//creating bitmap image
Bitmap bmp = new Bitmap("cells.bmp");
//FromImage method creates a new Graphics from the specified Image.
Graphics graphics = Graphics.FromImage(bmp);
// Create the Font object for the image text drawing.
Font font = new Font(fontname, fontsize);
// Instantiating object of Bitmap image again with the correct size for the text and font.
SizeF stringSize = graphics.MeasureString(txt, font);
bmp = new Bitmap(bmp, (int)stringSize.Width, (int)stringSize.Height);
graphics = Graphics.FromImage(bmp);
//Draw Specified text with specified format
graphics.DrawString(txt, font, Brushes.Red, 0, 0);
font.Dispose();
graphics.Flush();
graphics.Dispose();
bmp.Save("cells2.bmp", System.Drawing.Imaging.ImageFormat.Bmp);
return bmp; //return Bitmap Image
}
The output in the PictureBox is This
Now I've created myself multiple problems with the alignment, since I just pasted "bmp numbers" directly onto the "bmp cells"... is there a function of "mapping" the numbers correctly inside the cells, depending if its a single-digit or a two-digit number?
I'm pretty certain there's a way easier way to do all of this, maybe with the DataGridView? I only started using C#, any help would be appreciated.
The image "cells.bmp" is just a test image i created in Paint (xd), rather than that, I'm trying to write a function that will create the Bitmap cells through code
if (want_x_number_cells) {
//create bmp with appropriate number of cells
}
I'm trying to display the text property of a UltraButton to this unicode character- ▼ .
I've tried to copy this from the Character Map and also tried something like-
button1.Text = "\u2129"
The problem is both of them show the arrows in the designer mode in VS, but when I run the application, it shows an unrecognised symbol. I've gone through this link and this link, but the arrows only show up in the designer view, not while running the application. Why is this happening. Also, I've set the Font name to 'Arial Unicode MS'
I'm guessing the issue you are experiencing is unique to the UltraButton. From the looks of the image you just posted, you could probably get away with just using a standard Windows From Button. If you can, just open your ClassName.Designer.cs and find where your button text is being set. Copy the actual character into the text string:
this.YourButton.Text = "▼";
This shows up correctly in both the designer and when running the application.
If you really don't want to use a standard Windows Forms Button, you could always go about converting your text to an image and adding the image to the button. Would look something like this:
string text = "▼";
Font font = new Font("Arial Unicode MS", 12f);
Image img = new Bitmap(1, 1);
Graphics drawing = Graphics.FromImage(img);
SizeF textsize = drawing.MeasureString(text, font);
img.Dispose();
drawing.Dispose();
img = new Bitmap((int) textsize.Width, (int)textsize.Height);
drawing = Graphics.FromImage(img);
drawing.Clear(YourButton.BackColor);
Brush textBrush = new SolidBrush(Color.Black);
drawing.DrawString(text, font, textBrush, 0, 0);
drawing.Save();
textBrush.Dispose();
drawing.Dispose();
YourButton.Text = "";
YourButton.Image = img;
I can't seem to get the text I've written to show up on my image Here's the code I'm using
//Creates a bitmap with the path to the current image
Bitmap LabelImage = new Bitmap(dtImages.Rows[intCurrentImage]["ImageURL"].ToString());
Graphics graphic = Graphics.FromImage(LabelImage);
graphic.DrawString("Hello", new Font("Tahoma",40), Brushes.Azure, new System.Drawing.Point(0,0));
//put Image that I just created and put the text on into an Infragistics UltraPicureBox
picImage.Image = LabelImage
You did not update your original image (LabelImage), so why should the text you added to the Graphics object show up?.
From MSDN, Graphics.FromImage:
Creates a new Graphics from the specified Image.
(emphasis mine)
After you have added the text, you need to save the changes:
graphic.Save();
Unrelated to your question, you should really put the creation of the Graphics object in a using statement, to ensure proper disposal:
using(Graphics graphic = Graphics.FromImage(LabelImage))
{
// use graphic here
}
I just tried this
Bitmap bitmap = new Bitmap("C:\\Untitled.png");
Graphics g = Graphics.FromImage(bitmap);
g.DrawString("Hello", new Font("Tahoma", 40), Brushes.Azure, new System.Drawing.Point(0, 0));
pictureBox1.Image = bitmap;
and it works fine for me. Just try to pick a contrasting brush.