일부 쿼리 문자열 매개 변수를 사용하고 지정된 텍스트가있는 비트 맵을 반환하는 .ashx 페이지가 있습니다. 내가 가지고있는 문제는 현재 비트 맵의 크기를 100 X 100로 수동으로 설정하는 것입니다. 실제로 비트 맵을 작성하기 만하면 충분히 커야 모든 비트 맵을 포함 할 수 있습니다. 어떻게해야합니까?프로그래밍 방식으로 만든 비트 맵의 크기를 조정하여 그 위에 그려지는 텍스트를 일치시키는 방법은 무엇입니까?
public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "image/png";
string text = context.Request.QueryString["Text"];
//set FontName
string fontName;
if (context.Request.QueryString["FontName"] != null)
{
fontName = context.Request.QueryString["FontName"];
}
else
{
fontName = "Arial";
}
//Set FontSize
int fontEms;
if (context.Request.QueryString["FontSize"] != null)
{
string fontSize = context.Request.QueryString["FontSize"];
fontEms = Int32.Parse(fontSize);
}
else
{
fontEms = 12;
}
//Set Font Color
System.Drawing.Color color;
if (context.Request.QueryString["FontColor"] != null)
{
string fontColor = context.Request.QueryString["FontColor"];
color = System.Drawing.ColorTranslator.FromHtml(fontColor);
context.Response.Write(color.ToString());
}
else
{
color = System.Drawing.Color.Red;
}
using (System.Drawing.Text.PrivateFontCollection fnts = new System.Drawing.Text.PrivateFontCollection())
using (System.Drawing.FontFamily fntfam = new System.Drawing.FontFamily(fontName))
using (System.Drawing.SolidBrush brush = new System.Drawing.SolidBrush(color))
using (System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(100, 100))
{
using (System.Drawing.Font fnt = new System.Drawing.Font(fntfam, fontEms))
{
fnts.AddFontFile(System.IO.Path.Combine(@"C:\Development\Fonts\", fontName));
System.Drawing.Graphics graph = System.Drawing.Graphics.FromImage(bmp);
graph.DrawString(text, fnt, brush, new System.Drawing.Point(0, 0));
string imgPath = System.IO.Path.Combine(@"C:\Development\MyPath\Images\Text", System.IO.Path.GetRandomFileName());
bmp.Save(imgPath);
context.Response.WriteFile(imgPath);
}
}
}
업데이트 : 내가 필요한 것보다 비트 맵 더 큰 만드는 감아 , 즉 비트 맵의 그래픽을 얻고 내가 갈거야 텍스트를 쓸 수있는 크기와 다음 작물을 얻기 위해 MeasureString 방법을 사용 비트 맵의 해당 부분을 새로운 비트 맵으로 변환하십시오. 내가 추측하는 것
using (System.Drawing.Text.PrivateFontCollection fnts = new System.Drawing.Text.PrivateFontCollection())
using (System.Drawing.FontFamily fntfam = new System.Drawing.FontFamily(fontName))
using (System.Drawing.SolidBrush brush = new System.Drawing.SolidBrush(color))
using (System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(500, 500))
{
using (System.Drawing.Font fnt = new System.Drawing.Font(fntfam, fontEms))
{
fnts.AddFontFile(System.IO.Path.Combine(@"C:\Development\Fonts\", fontName));
System.Drawing.Graphics graph = System.Drawing.Graphics.FromImage(bmp);
System.Drawing.SizeF bmpSize = graph.MeasureString(text, fnt);
System.Drawing.Rectangle rect = new System.Drawing.Rectangle(new System.Drawing.Point(0, 0), bmpSize.ToSize());
System.Drawing.Bitmap croppedBmp = bmp.Clone(rect, bmp.PixelFormat);
graph = System.Drawing.Graphics.FromImage(croppedBmp);
graph.DrawString(text, fnt, brush, new System.Drawing.Point(0, 0));
string imgPath = System.IO.Path.Combine(@"C:\Development\MyPath\Images\Text", System.IO.Path.GetRandomFileName());
croppedBmp.Save(imgPath);
context.Response.WriteFile(imgPath);
}
}
+1 흥미로운 질문입니다. =) –