我如何从图片框C#中的文本输入中心对齐拉链BMP

发布于 2025-01-19 16:05:53 字数 2349 浏览 0 评论 0原文

我是一个初学者,正在编写一个简单的 Windows 桌面表单应用程序,其中包含用户输入的文本框,然后单击按钮将该文本转换为图片框中显示的位图(在 LED 显示面板上显示时只有 96 x 64 像素) )。

从 Stack Overflow 借用了一些很好的例子来获得一个可行的解决方案。 一切正常,除了无法居中对齐。

我可以让它左对齐很好,但是当我添加新的创建字符串格式以居中对齐和行居中对齐时,文本会进一步向左和向上移动 - 不确定是否是图像填充?

这是我到目前为止所拥有的,

    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 似乎并不重要。非常感谢某人的帮助。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

Smile简单爱 2025-01-26 16:05:53

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;

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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文