Home > Mobile >  how do I center align drawstring bmp from text input in picturebox c#
how do I center align drawstring bmp from text input in picturebox c#

Time:04-08

I'm a beginner writing a simple windows desktop form app with a textbox that user types into and then clickbutton converts that text into a bitmap that is shown in a picturebox (that is only 96 x 64 pixels as it displays on an LED display panel).

Have borrowed some great examples from Stack overflow to get to a working solution. All works fine except it won't center align.

I can get it to left align fine but when I add in the new Create a stringformat to center align and line center align, the text then moves even further left and up - not sure whether its the image padding?

Here's what I have so far

    private void button18_Click(object sender, EventArgs e)
    {
        //dispose previous image
        File.Delete("myimage.bmp");

        // Our text to paint
        String str = textBox1.Text;

        // Create our new bitmap object
        Bitmap bmp = new Bitmap(96,64);
        Image img = Image.FromHbitmap(bmp.GetHbitmap());

        // Get our graphics object
        Graphics g = Graphics.FromImage(img);
        g.Clear(Color.Transparent);

        // Define our image padding
        var imgPadding = new Rectangle(1, 1, 1, 1);

        // Determine the size of our text, using our specified font.
        Font ourFont = new Font(
            FontFamily.GenericSansSerif,
            10.0f,
            FontStyle.Regular,
            GraphicsUnit.Point
        );
        SizeF strSize = g.MeasureString(
            str,
            ourFont,
            (bmp.Width - imgPadding.Left - imgPadding.Right),
            StringFormat.GenericDefault
        );
        // Create a StringFormat object with the each line of text, and the block
        // of text centered on the page.
        StringFormat stringFormat = new StringFormat();
        stringFormat.Alignment = StringAlignment.Center;
        stringFormat.LineAlignment = StringAlignment.Center;

        // Create our brushes
        SolidBrush textBrush = new SolidBrush(Color.White);

        // Draw our string to the bitmap using our graphics object
        g.DrawString(str, ourFont, textBrush, imgPadding.Left, imgPadding.Top, stringFormat);

        // Flush
        g.Flush(System.Drawing.Drawing2D.FlushIntention.Sync);

        // Save our image.
        img.Save("myImage.bmp", System.Drawing.Imaging.ImageFormat.Bmp);

        // Clean up
        textBrush.Dispose();
        g.Dispose();
        bmp.Dispose();

        //showimageon screen
        Image img1;
        using (var bmpTemp = new Bitmap("myImage.bmp"))
        {
            img1 = new Bitmap(bmpTemp);
        }
        pictureBox4.Image = img1;

It doesn't seem to matter if I have the stringformat.genericdefault there or not. Would really appreciate someone's assistance.

CodePudding user response:

imgPadding should have same width and height as the image.

I did a little rewriting of your code to make it work:

//dispose previous image
File.Delete("myimage.bmp");

// Our text to paint
String str = textBox1.Text;

// Create our new bitmap object
Bitmap bmp = new Bitmap(96, 64);
Image img = Image.FromHbitmap(bmp.GetHbitmap());

// Get our graphics object
Graphics g = Graphics.FromImage(img);
g.Clear(Color.Transparent);

// Define our image padding and set same with/height as image.
var imgPadding = new Rectangle(0, 0, bmp.Width, bmp.Height);

// Determine the size of our text, using our specified font.
Font ourFont = new Font(
    FontFamily.GenericSansSerif,
    10.0f,
    FontStyle.Regular,
    GraphicsUnit.Point
);
// Create a StringFormat object with the each line of text, and the block
// of text centered on the page.
StringFormat stringFormat = new StringFormat() { Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center };

// Create our brushes
SolidBrush textBrush = new SolidBrush(Color.White);

// Draw our string to the bitmap using our graphics object
g.DrawString(str, ourFont, textBrush, imgPadding, stringFormat);

// Flush
g.Flush(System.Drawing.Drawing2D.FlushIntention.Sync);

// Save our image.
img.Save("myImage.bmp", System.Drawing.Imaging.ImageFormat.Bmp);

// Clean up
textBrush.Dispose();
g.Dispose();
bmp.Dispose();

//showimageon screen
Image img1;
using (var bmpTemp = new Bitmap("myImage.bmp"))
{
    img1 = new Bitmap(bmpTemp);
}
pictureBox4.Image = img1;
  • Related