我如何从图片框C#中的文本输入中心对齐拉链BMP
我是一个初学者,正在编写一个简单的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
imgPadding 应与图像具有相同的宽度和高度。
我对您的代码进行了一些重写以使其正常工作:
imgPadding should have same width and height as the image.
I did a little rewriting of your code to make it work: