我有以下问题。我想在位图图像中制作一些图形,如债券形式
我可以在图像中写一个文字
但我会在不同的位置写更多的文字
Bitmap a = new Bitmap(@"path\picture.bmp");
using(Graphics g = Graphics.FromImage(a))
{
g.DrawString(....); // requires font, brush etc
}
如何编写文本并保存并在保存的图像中写入另一个文本
答案 0 :(得分:65)
要绘制多个字符串,请多次调用graphics.DrawString
。您可以指定绘制字符串的位置。这个例子我们将绘制两个字符串“Hello”,“Word”(蓝色前面的“Hello”,红色的“Word”):
string firstText = "Hello";
string secondText = "World";
PointF firstLocation = new PointF(10f, 10f);
PointF secondLocation = new PointF(10f, 50f);
string imageFilePath = @"path\picture.bmp"
Bitmap bitmap = (Bitmap)Image.FromFile(imageFilePath);//load the image file
using(Graphics graphics = Graphics.FromImage(bitmap))
{
using (Font arialFont = new Font("Arial", 10))
{
graphics.DrawString(firstText, arialFont, Brushes.Blue, firstLocation);
graphics.DrawString(secondText, arialFont, Brushes.Red, secondLocation);
}
}
bitmap.Save(imageFilePath);//save the image file
编辑“我添加了一个加载并保存代码”。
您可以随时Image.FromFile
打开位图文件,并使用上面的代码在其上绘制新文本。然后保存图像文件bitmap.Save
答案 1 :(得分:3)
以下是Graphics.DrawString
来自here的调用示例:
g.DrawString("My\nText", new Font("Tahoma", 40), Brushes.White, new PointF(0, 0));
它显然依赖于安装了一个名为Tahoma
的字体。
Brushes
类有许多内置画笔。
另请参阅Graphics.DrawString
的MSDN页面。
答案 2 :(得分:1)
要保存对同一文件的更改,我必须在Jalal Said问题上合并NSGaga的答案和this的答案。您需要基于旧的 Bitmap 对象创建一个新的 Bitmap 对象,配置旧的 Bitmap 对象,然后使用新对象进行保存:
string firstText = "Hello";
string secondText = "World";
PointF firstLocation = new PointF(10f, 10f);
PointF secondLocation = new PointF(10f, 50f);
string imageFilePath = @"path\picture.bmp";
Bitmap newBitmap;
using (var bitmap = (Bitmap)Image.FromFile(imageFilePath))//load the image file
{
using(Graphics graphics = Graphics.FromImage(bitmap))
{
using (Font arialFont = new Font("Arial", 10))
{
graphics.DrawString(firstText, arialFont, Brushes.Blue, firstLocation);
graphics.DrawString(secondText, arialFont, Brushes.Red, secondLocation);
}
}
newBitmap = new Bitmap(bitmap);
}
newBitmap.Save(imageFilePath);//save the image file
newBitmap.Dispose();
答案 3 :(得分:0)
public string imageFilePath = null;
public string textOnImage = null;
public Image baseImage;
public Image modifiedImage;
public int xcoOrdinate = 0;
public int ycoOrdinate = 0;
public Form1()
{
InitializeComponent();
}
private void buttonLoadImage_Click(object sender, EventArgs e)
{
try
{
OpenFileDialog uploadfileDialog = new OpenFileDialog();
uploadfileDialog.Filter = "All Files (*.*)|*.*";
uploadfileDialog.Multiselect = false;
if (uploadfileDialog.ShowDialog() == DialogResult.OK)
{
imageFilePath = uploadfileDialog.FileName;
}
baseImage = Image.FromFile(imageFilePath);
modifiedImage = (Image)baseImage.Clone();
pictureBoxToShowPic.Image = baseImage;
}
catch (Exception ex)
{
MessageBox.Show(ex.Source + " : " + ex.Message);
}
}
public void paint()
{
try
{
Graphics g = Graphics.FromImage(modifiedImage);
using (Font myfont = new Font("Arial", 14))
{
var format = new StringFormat
{
Alignment = StringAlignment.Center,
LineAlignment = StringAlignment.Center
};
g.DrawString(textOnImage, myfont, Brushes.Black, new Point(xcoOrdinate, ycoOrdinate), format);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Source + " : " + ex.Message);
}
}
private void buttonAddText_Click(object sender, EventArgs e)
{
try
{
textOnImage = textBoxWriteText.Text;
paint();
pictureBoxToShowPic.Image = modifiedImage;
pictureBoxToShowPic.Refresh();
}
catch (Exception ex)
{
MessageBox.Show(ex.Source + " : " + ex.Message);
}
}
private void pictureBoxToShowPic_MouseDoubleClick(object sender, MouseEventArgs e)
{
try
{
xcoOrdinate = e.X;
ycoOrdinate = e.Y;
}
catch (Exception ex)
{
MessageBox.Show(ex.Source + " : " + ex.Message);
}
}
private void buttonSaveImage_Click(object sender, EventArgs e)
{
try
{
SaveFileDialog savefileDialog = new SaveFileDialog();
savefileDialog.Filter = "Images|*.jpg ; *.png ; *.bmp";
if (savefileDialog.ShowDialog() == DialogResult.OK)
{
imageFilePath = savefileDialog.FileName;
}
modifiedImage.Save(imageFilePath);
}
catch (Exception ex)
{
MessageBox.Show(ex.Source + " : " + ex.Message);
}
}
答案 4 :(得分:-2)
如果有人对此代码行有疑问:
using(Graphics graphics = Graphics.FromImage(bitmap))
解决方案是:
Bitmap bitmap = (Bitmap)**System.Drawing.Image.FromFile**(@"C:\Documents and Settings\", true);