에서 런타임에. 내가 런타임에 PNG로 파일에 텍스트를 그릴 수 있어야합니다 2,010 대 C#을 윈폼
응용 프로그램 시작을 (의 WinForm 응용 프로그램 2010 C#)를 이미지를 수정하고 도구 모음
고객을로드 고객이 수정이 아이콘의 botton을 코너를 왼쪽으로 숫자 나 문자 하나를 추가해야 도구 모음에서 아이콘을로드 할 때이 시점에서
를 검색합니다. PNG (파일)
내가 함께 연주 한 내가 할 수있는 것을보기위한 이상한 앱.하지만 나는 기쁘다. 몇 가지 문제. ,
- 어떻게 (내 테스트에 하드 코딩하고)
- 내가 텍스트를 그릴 수 있어요. 내 PNG 파일의 경로를받을 수 있나요하지만 내가 도구 모음을 다시로드해야 추측 displayed.I 아니다 아니?
- 2 번 이상 아이콘에 글자를 쓸 때 이전 글자 위에 씁니다. 이전 글자를 지우거나 지우고 새 글자를 지우고 싶습니다.
의견이 있으십니까?
내 임시 코드 :
private void button1_Click(object sender, EventArgs e)
{
string filename = @"c:\WinFormTest\Resources\column.png";
var bitmapImage = new Bitmap(Properties.Resources.column);
bitmapImage.DrawText("A", new Font("Arial", 8), Brushes.Black, new RectangleF(0, 0, 500, 500), filename);
}
}
public static class ImageExtensions
{
public static Bitmap DrawText(this Bitmap image,string textToDraw,Font font,Brush brush,RectangleF rectangleF,string filename="")
{
if(image==null) throw new ArgumentNullException();
if(font==null)throw new ArgumentNullException();
if (brush == null) throw new ArgumentNullException();
if (rectangleF == null) throw new ArgumentNullException();
var format = filename.GetImageFormat();
var newBitmap = new Bitmap(image, image.Width, image.Height);
using (var graphics = Graphics.FromImage(newBitmap))
{
graphics.DrawString(textToDraw, font, brush, rectangleF);
}
if (!string.IsNullOrEmpty(filename))
{newBitmap.Save(filename, format);}
return newBitmap;
}
public static ImageFormat GetImageFormat(this string fileName)
{
if (string.IsNullOrEmpty(fileName))
return ImageFormat.Bmp;
if (fileName.EndsWith("jpg", StringComparison.InvariantCultureIgnoreCase) || fileName.EndsWith("jpeg", StringComparison.InvariantCultureIgnoreCase))
return ImageFormat.Jpeg;
if (fileName.EndsWith("png", StringComparison.InvariantCultureIgnoreCase))
return ImageFormat.Png;
if (fileName.EndsWith("tiff", StringComparison.InvariantCultureIgnoreCase))
return ImageFormat.Tiff;
if (fileName.EndsWith("ico", StringComparison.InvariantCultureIgnoreCase))
return ImageFormat.Icon;
if (fileName.EndsWith("gif", StringComparison.InvariantCultureIgnoreCase))
return ImageFormat.Gif;
return ImageFormat.Bmp;
}
}
@ BlueMonkMN. 시간과 코드에 감사한다. 새로운 고객을로드 할 때마다 적절한 문자 또는 숫자로 아이콘을 수정해야합니다. 코드를 실행하려면 "myImage"가 내 새 이미지를 참조해야합니까? 오버 라이드에 대해 잘 모름 OnPaint.thanks – user9969
Ii가 OnPaint 함수로 테스트했습니다. 무시해. 나는 그것을 지금 삭제했다. – BlueMonkMN