首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在picturebox c#中对齐来自文本输入的绘图字符串bmp

如何在picturebox c#中对齐来自文本输入的绘图字符串bmp
EN

Stack Overflow用户
提问于 2022-04-07 12:51:04
回答 1查看 238关注 0票数 1

我是一个初学者,编写了一个简单的windows桌面表单应用程序,用户可以输入文本框,然后单击按钮将文本转换为picturebox中显示的位图(当它显示在LED显示屏上时,只有96x64像素)。

已经从Stack溢出中借用了一些很好的例子来获得一个有效的解决方案。所有的工作正常,除了它不会中心对齐。

我可以让它左对齐很好,但当我添加新的创建一个字符串格式的中心对齐和线中心对齐,然后文本移动到更左和上-不确定它是否是图像填充?

到目前为止,我的情况如下

代码语言:javascript
运行
复制
    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似乎并不重要。会很感激别人的帮助。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-04-07 13:53:34

imgPadding应该具有与图像相同的宽度和高度。

我对您的代码做了一些重写以使其工作:

代码语言:javascript
运行
复制
//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;
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71782510

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档