Click here to Skip to main content
15,997,776 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
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);
}
Posted
Updated 21-Nov-13 5:22am
v5
Comments
Sergey Alexandrovich Kryukov 19-Nov-13 18:41pm    
http://www.whathaveyoutried.com so far? At least you could do some Web search.
—SA
JoCodes 19-Nov-13 23:00pm    
Where are you stuck? any related code which you have tried ?
Kalyan Ramisetti 20-Nov-13 10:58am    
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);

1 solution

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
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900