簡體   English   中英

如何從圖片框 c# 中的文本輸入居中對齊拉繩 bmp

[英]how do I center align drawstring bmp from text input in picturebox c#

我是一個初學者,正在編寫一個簡單的 windows 桌面表單應用程序,其中包含用戶輸入的文本框,然后單擊按鈕將該文本轉換為圖片框中顯示的 bitmap(在 LED 顯示面板上顯示時僅為 96 x 64 像素) ).

從 Stack overflow 中借用了一些很好的例子來找到一個可行的解決方案。 一切正常,除了它不會居中對齊。

我可以讓它很好地左對齊,但是當我添加新的 Create a stringformat to center align 和 line center align 時,文本然后向左和向上移動得更遠 - 不確定它是否是圖像填充?

這是我到目前為止所擁有的

    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;

我是否有 stringformat.genericdefault 似乎並不重要。 非常感謝有人的幫助。

imgPadding 應與圖像具有相同的寬度和高度。

我對您的代碼做了一些重寫以使其工作:

//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;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM