带有轮廓和阴影的文本到图像asp.net c# [英] text to image with both outline and shadow asp.net c#
问题描述
我正在尝试使用asp.net c#将文本转换为带有轮廓和阴影的图像但是失败了。如果有人知道请帮助我。
到目前为止我试过这个,但是这个代码我得到了阴影,但我没有得到大纲。有拉绳类我正在获得阴影和drawpath类我正在获得轮廓。但我想要轮廓和阴影。如果阴影即将来临轮廓不会来,反之亦然。
试试
{
lblwidth.Text = txtwidth.Text;
lblheight.Text = txtheight.Text;
string text = txttext.Text;
int textLength = text.Length;
int fontSize = 200;
int orientation = 1;
int antialias = 1;
//设置画布宽度&高度
int width;
int height;
if(orientation == 1)
{
width =(fontSize * textLength) - ((textLength * fontSize)/ 3);
height = fontSize + 20;
}
else
{
width = fontSize + 20;
height =(int)(fontSize * textLength * 1.5);
}
//初始化图形
Pen blackPen = new Pen(Color.Black, 5);
RectangleF rectF = new RectangleF(0,0,width,height);
Bitmap pic = new Bitmap(width,height,PixelFormat.Format24bppRgb);
图形g = Graphics.FromImage(pic);
g.SmoothingMode = SmoothingMode.AntiAlias;
if(antialias == 1)g.TextRenderingHint = TextRenderingHint.AntiAlias;
//设置颜色
string fgCol或= ddlMultiColor.SelectedItem.Text;
string bgColor =White;
颜色fontColor = Color.FromName(fgColor);
颜色rectColor = Color.FromName(bgColor);
SolidBrush fgBrush = new SolidBrush(fontColor);
SolidBrush bgBrush = new SolidBrush(rectColor);
//矩形还是椭圆?
int bound = 1;
if(bound == 1)
{
g.FillRectangle(bgBrush,rectF);
}
else
{
g.FillRectangle(新的SolidBrush(Color.White),rectF);
g.FillEllipse(bgBrush,rectF);
}
////设置字体样式
FontStyle样式= FontStyle.Bold;
字体字体=新字体(ddlfont.SelectedItem .Text,fontSize,style,GraphicsUnit.Pixel);
//设置字体方向和对齐方式
StringFormat format = new StringFormat() ;
int reverse = 1;
if(reverse == 1 && orientation == 1)
{
format.FormatFlags = StringFormatFlags.DirectionRightToLeft;
}
else if(reverse == 1 && orientation> 1)
{
StringBuilder temp = new StringBuilder();
for(int i = textLength - 1; i> = 0;我 - )
{
temp.Insert((textLength - 1) - i,text [i]);
}
text = temp.ToString();
}
if(orientation> 1)
{
rectF.X = width / 4;
rectF.Width = fontSize - (fontSize / 4);
}
int alignment = 2;
if(alignment == 1)
{
format.Alignment = StringAlignment.Near;
}
else if(alignment == 2)
{
format.Alignment = StringAlignment.Center;
}
其他
{
format.Alignment = StringAlignment.Far;
}
format.LineAlignment = StringAlignment.Center;
//绘制任何阴影
int dropShadow = Convert.ToInt32(DropDownList1.SelectedValue);
if(dropShadow> 0)
{
颜色shadowColor = Color.FromName(ddlMultiColor2.SelectedItem.Text);
开关(dropShadow)
{
案例1:
rectF.Offset(-5,-5);
g.DrawString(文本,字体,新的SolidBrush(shadowColor),rectF,格式);
rectF.Offset(+ 5,+ 5);
break;
案例2:
rectF.Offset(+ 5,-5);
g.DrawString(text, font,new SolidBrush(shadowColor),rectF,format);
rectF.Offset(-5,+ 5);
break;
案例3:
rectF.Offset(-5,+ 5);
g.DrawString(text,font,new Sol idBrush(shadowColor),rectF,format);
rectF.Offset(+ 5,-5);
break;
案例4:
rectF.Offset(+ 5,+ 5);
g.DrawString(text,font,new SolidBrush(shadowColor),rectF ,格式);
rectF.Offset(-5,-5);
休息;
}
}
g.SmoothingMode = SmoothingMode.HighQuality;
/ / GraphicsPath fontpath = new GraphicsPath();
//fontpath.AddString(txttext.Text,new FontFamily(ddlfont.SelectedItem.Text),(int)FontStyle.Bold,200,new Point(0 ,0),StringFormat.GenericDefault);
//颜色bor = Color.FromName(ddlMultiColor.SelectedItem.Text);
/ ///刷br =(颜色)ddlMult iColor.SelectedItem.Text;
//g.FillPath(new SolidBrush(bor),fontpath);
//g.DrawPath (新笔(Color.Black,10),fontpath);
//最后,绘制字体
g.DrawString(text,font,fgBrush,rectF,format );
////Response.ContentType =image / jpeg;
////pic.Save(Response.OutputStream,ImageFormat.Jpeg);
string fileName = text +.jpg;
pic.Save(Server.MapPath(〜/ images /)+ fileName,ImageFormat.Jpeg);
imagetext.ImageUrl =〜/ images /+ fileName;
imagetext.Visible = true;
pic.Dispose();
}
catch(例外情况)
{
Response.Write(ex.Message);
}
请参阅此CodeProject文章:大纲文本 [ ^ ]。
这与ASP.NET无关。一旦你可以在服务器端的位图上渲染你的图形,你就可以在你的页面上显示它。
-SA
I'm trying to convert text to image with both outline and shadow using asp.net c# but failed.If anyone knows please help me.
So far i tried this,but with this code i'm getting shadow but i'm not getting outline.With drawstring class i'm getting shadow and with drawpath class i'm getting outline.But i want both outline and shadow.Here if shadow is coming outline was not coming and viceversa.
try
{
lblwidth.Text = txtwidth.Text;
lblheight.Text = txtheight.Text;
string text = txttext.Text;
int textLength = text.Length;
int fontSize = 200;
int orientation = 1;
int antialias = 1;
// Set canvas width & height
int width;
int height;
if (orientation == 1)
{
width = (fontSize * textLength) - ((textLength * fontSize) / 3);
height = fontSize + 20;
}
else
{
width = fontSize + 20;
height = (int)(fontSize * textLength * 1.5);
}
// Initialize graphics
Pen blackPen = new Pen(Color.Black, 5);
RectangleF rectF = new RectangleF(0, 0, width, height);
Bitmap pic = new Bitmap(width, height, PixelFormat.Format24bppRgb);
Graphics g = Graphics.FromImage(pic);
g.SmoothingMode = SmoothingMode.AntiAlias;
if (antialias == 1) g.TextRenderingHint = TextRenderingHint.AntiAlias;
// Set colors
string fgColor = ddlMultiColor.SelectedItem.Text;
string bgColor = "White";
Color fontColor = Color.FromName(fgColor);
Color rectColor = Color.FromName(bgColor);
SolidBrush fgBrush = new SolidBrush(fontColor);
SolidBrush bgBrush = new SolidBrush(rectColor);
// Rectangle or ellipse?
int bound = 1;
if (bound == 1)
{
g.FillRectangle(bgBrush, rectF);
}
else
{
g.FillRectangle(new SolidBrush(Color.White), rectF);
g.FillEllipse(bgBrush, rectF);
}
//// Set font style
FontStyle style = FontStyle.Bold;
Font font = new Font(ddlfont.SelectedItem.Text, fontSize, style, GraphicsUnit.Pixel);
// Set font direction & alignment
StringFormat format = new StringFormat();
int reverse = 1;
if (reverse == 1 && orientation == 1)
{
format.FormatFlags = StringFormatFlags.DirectionRightToLeft;
}
else if (reverse == 1 && orientation > 1)
{
StringBuilder temp = new StringBuilder();
for (int i = textLength - 1; i >= 0; i--)
{
temp.Insert((textLength - 1) - i, text[i]);
}
text = temp.ToString();
}
if (orientation > 1)
{
rectF.X = width / 4;
rectF.Width = fontSize - (fontSize / 4);
}
int alignment = 2;
if (alignment == 1)
{
format.Alignment = StringAlignment.Near;
}
else if (alignment == 2)
{
format.Alignment = StringAlignment.Center;
}
else
{
format.Alignment = StringAlignment.Far;
}
format.LineAlignment = StringAlignment.Center;
// Draw any drop-shadow
int dropShadow = Convert.ToInt32(DropDownList1.SelectedValue);
if (dropShadow > 0)
{
Color shadowColor = Color.FromName(ddlMultiColor2.SelectedItem.Text);
switch (dropShadow)
{
case 1:
rectF.Offset(-5, -5);
g.DrawString(text, font, new SolidBrush(shadowColor), rectF, format);
rectF.Offset(+5, +5);
break;
case 2:
rectF.Offset(+5, -5);
g.DrawString(text, font, new SolidBrush(shadowColor), rectF, format);
rectF.Offset(-5, +5);
break;
case 3:
rectF.Offset(-5, +5);
g.DrawString(text, font, new SolidBrush(shadowColor), rectF, format);
rectF.Offset(+5, -5);
break;
case 4:
rectF.Offset(+5, +5);
g.DrawString(text, font, new SolidBrush(shadowColor), rectF, format);
rectF.Offset(-5, -5);
break;
}
}
g.SmoothingMode = SmoothingMode.HighQuality;
//GraphicsPath fontpath = new GraphicsPath();
//fontpath.AddString(txttext.Text, new FontFamily(ddlfont.SelectedItem.Text), (int)FontStyle.Bold, 200, new Point(0, 0), StringFormat.GenericDefault);
//Color bor = Color.FromName(ddlMultiColor.SelectedItem.Text);
////Brush br = (Color)ddlMultiColor.SelectedItem.Text;
//g.FillPath(new SolidBrush(bor), fontpath);
//g.DrawPath(new Pen(Color.Black, 10), fontpath);
// Finally, draw the font
g.DrawString(text, font, fgBrush, rectF, format);
////Response.ContentType = "image/jpeg";
////pic.Save(Response.OutputStream, ImageFormat.Jpeg);
string fileName = text + ".jpg";
pic.Save(Server.MapPath("~/images/") + fileName, ImageFormat.Jpeg);
imagetext.ImageUrl = "~/images/" + fileName;
imagetext.Visible = true;
pic.Dispose();
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
Please see this CodeProject article: Outline Text[^].
This is not related to ASP.NET. Once you can render your graphics on a bitmap on the server side, you can show it on your page.
—SA
这篇关于带有轮廓和阴影的文本到图像asp.net c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!