我如何从 picturebox c# 中的文本输入居中对齐 drawstring bmp
how do I center align drawstring bmp from text input in picturebox c#
我是一个初学者,正在编写一个简单的 windows 桌面表单应用程序,其中包含一个文本框,用户可以在其中输入文本框,然后单击按钮将该文本转换为显示在图片框中的位图(只有 96 x 64 LED 显示面板上显示的像素)。
从 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似乎都没有关系。非常感谢有人的帮助。
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;
我是一个初学者,正在编写一个简单的 windows 桌面表单应用程序,其中包含一个文本框,用户可以在其中输入文本框,然后单击按钮将该文本转换为显示在图片框中的位图(只有 96 x 64 LED 显示面板上显示的像素)。
从 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似乎都没有关系。非常感谢有人的帮助。
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;