Để vẽ nhiều chuỗi, hãy gọi graphics.DrawString
nhiều lần. Bạn có thể chỉ định vị trí của chuỗi được vẽ. Ví dụ này, chúng tôi sẽ vẽ hai chuỗi "Xin chào", "Từ" ("Xin chào" màu xanh lam phía trước "Word" màu đỏ):
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
Chỉnh sửa: "Tôi Thêm tải và lưu mã".
Bạn có thể mở tệp bitmap bất kỳ lúc nào Image.FromFile
và vẽ văn bản mới trên đó bằng đoạn mã trên. và sau đó lưu tệp hình ảnhbitmap.Save