Dynamically generated Bitmap image with aligned data in it - c#

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
}

Related

Converting Scanned PDF's to an Image

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

How to save the painted control as a bitmap?

I have a picturebox (pictureBox1), and it gets lines drawn on it, from the paint event. I was wondering how to convert that drawing (with the lines) to a bitmap, and save it as a file. I've tried:
Bitmap bmp = new Bitmap(pictureBox1.Width, pictureBox.Height);
pictureBox1.DrawToBitmap(bmp, pictureBox1.Bounds);
bmp.Save("MyImage.bmp");
But that is a blank image, without the lines. Does anyone know how I can save it with the lines? Thank you.
int bitmapX = 100
int bitmapY = 100
Bitmap imgToSave = new Bitmap(bitmapX, bitmapY);
Graphics gfx = Graphics.FromImage(imgToSave);
// draw on the image with
gfx.DrawLine() // or whatever you want to draw
// save the screenshot
string saveFilePath = Application.StartupPath + "\<name of the image>.png";
imgToSave.Save(saveFilePath, ImageFormat.Png);
This is a code snippet that works for me.
Don't use the PictureBox.Bounds property but instead use a simple Rectangle object/structure filled with the `PictureBox' width and height like this:
var bitmap = new Bitmap(pictureBox1.Width, pictureBox1.Height);
pictureBox1.DrawToBitmap(bitmap, new Rectangle(0, 0, pictureBox1.Width, pictureBox1.Height));
bitmap.Save("c:\\temp\\image.bmp");
Using the Bounds property you are fetching the correct size of the picture box control, but depending on the position not the 0,0 coordinate, but the control position, which leads to an empty bitmap.

Generating tiff file from a multi-line text box in C#

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.
}

Determine file type from ImageFormat.MemoryBMP

After resizing an image, my resize function returns a newly drawn Image. I'm running into an issue where I need to determine what the file extension of the returned Image should be. I was using the Image.RawFormat property previously but everytime an Image is returned from this function it has ImageFormat.MemoryBMP, rather than ImageFormat.Jpeg or ImageFormat.Gif for example.
So basically my question is, how can I determine what file type the newly resized Image should be?
public static Image ResizeImage(Image imageToResize, int width, int height)
{
// Create a new empty image
Image resizedImage = new Bitmap(width, height);
// Create a new graphic from image
Graphics graphic = Graphics.FromImage(resizedImage);
// Set graphics modes
graphic.SmoothingMode = SmoothingMode.HighQuality;
graphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphic.PixelOffsetMode = PixelOffsetMode.HighQuality;
// Copy each property from old iamge to new image
foreach (var prop in imageToResize.PropertyItems)
{
resizedImage.SetPropertyItem(prop);
}
// Draw the new Image at the resized size
graphic.DrawImage(imageToResize, new Rectangle(0, 0, width, height));
// Return the new image
return resizedImage;
}
The resized image is not in any file based format, it is an uncompressed memory representation of the pixels in the image.
To save this image back to disk the data needs to be encoded in the selected format, which you have to specify. Look at the Save method, it takes an ImageFormat as a second argument, make that Jpeg or whatever format best fits your application.

Accurate BoundingBox on Microsoft ISF?

I have a set of Microsoft ISF (Ink Serialized Format) images, which I am attempting to convert to PNGs to include in a web page. I have successfully used the C# package Microsoft.Ink to draw the ink to a Bitmap and save as PNG:
byte[] data; // data has the raw bytes of the ISF file
Ink ink = new Ink();
Renderer renderer = new Renderer();
Stream outStream; // a Stream to hold the PNG data
ink.Load(data);
using (Strokes strokes = ink.Strokes) {
// get bounds of ISF
rect = strokes.GetBoundingBox(BoundingBoxMode.PointsOnly);
// create bitmap matching bounds
bm = new Bitmap(rect.Width, rect.Height);
// draw the ISF onto the Bitmap
using (Graphics g = Graphics.FromImage(bm)) {
g.Clear(Color.Transparent);
renderer.Draw(g, strokes);
}
}
// save the Bitmap to PNG format
bm.Save(outStream, System.Drawing.Imaging.ImageFormat.Png);
... however, the image dimensions seem to be abnormally large. Changing the BoundingBoxMode enum passed in to the GetBoundingBox method does not appear to change anything, and I'm getting images that are 465px × 660px and contain only a handwritten letter 'a' taking up maybe 25px × 30px in actual space.
Any suggestions on how to get a more accurate bounding box?
The bounding box rectangle is in "Inkspace" coordinates - create throwaway bitmap and graphics objects, then convert the rectangle using renderer.InkSpacetoPixel.
Add these lines between the GetBoundingBox call and the creation of the Bitmap bm, and the Bitmap should be sized correctly:
Bitmap bmTrash = new Bitmap(10, 10);
Graphics gTrash = Graphics.FromImage(bmTrash);
renderer.InkSpaceToPixel(gTrash, rect.Location);
renderer.InkSpaceToPixel(gTrash, rect.Size);

Categories

Resources