.Net의 System.Drawing.Graphics.DrawString() 메소드를 사용하여 이미지 위에 텍스트를 그려서 새 파일에 저장합니다..Net의 이미지에 스트로크로 텍스트를 그립니다.
// define image file paths
string sourceImagePath = HttpContext.Current.Server.MapPath("~/img/sourceImage.jpg");
string destinationImagePath = HttpContext.Current.Server.MapPath("~/img/") + "finalImage.jpg";
// create new graphic to draw to
Bitmap bm = new Bitmap(200, 200);
Graphics gr = Graphics.FromImage(bm);
// open and draw image into new graphic
System.Drawing.Image sourceImage = System.Drawing.Image.FromFile(sourceImagePath, true);
gr.DrawImage(sourceImage, 0, 0);
sourceImage.Dispose();
// write "my text" on center of image
gr.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Center;
PrivateFontCollection fontCollection = new PrivateFontCollection();
fontCollection.AddFontFile(HttpContext.Current.Server.MapPath("~/fonts/HelveticaNeueLTStd-BlkCn.ttf"));
// prototype (1)
gr.DrawString("my text", new Font(fontCollection.Families[0], 14, FontStyle.Bold), new SolidBrush(Color.FromArgb(255, 255, 255)), 100, 0, sf);
fontCollection.Dispose();
// save new image
if (File.Exists(destinationImagePath))
{
File.Delete(destinationImagePath);
}
bm.Save(destinationImagePath);
bm.Dispose();
gr.Dispose();
이미지에 텍스트를 추가하는 방법은 텍스트를 선을 추가하는 방법을 제공하지 않습니다. 예를 들어, 실제로해야 할 일은 이미지에 쓰여진 텍스트에 특정 색상의 2px 스트로크를 추가하는 것입니다.
.Net Framework < = v3.5로 어떻게 수행 할 수 있습니까?
매력처럼 작동합니다! 내가 선택한 한 가지는 먼저 DrawPath 호출을 수행 한 다음 FillPath를 수행하는 것입니다. 이렇게하면 드로우 된 패스가 실제 채워진 패스를 덮지 않고 더 많은 획을 긋습니다. 감사합니다. – Rafe