Answered by:
How to dynamic make a string to be graphics ?

Question
-
User-1655262121 posted
Hello:
I would like to dynamicakky make a string to be a graphics file (jpg file) in asp.net with c# as below PHP code but the problem is that I need to know
the graphic size to create a graphic object first.
---------------------following is PHP code-------------------------------
<?php
function mfont($message)
{
$BASEDIR='mfont/'; //font folder
$fs=16; //font size
$fontsize=(int) $_REQUEST['fsize'];
if ($fontsize<=6|| $fontsize>100) $fontsize=$fs;
$fontfile='twu3.ttf'; //fontfile
if(strlen(trim($message))==0)
return $message;
$restxt='';
$TEXTL = split ("[\r\n]", $message);
for ($j=0;$j<count($TEXTL);$j++)
{
if(strlen(trim($TEXTL[$j]))==0) continue;
$TEXTA = split (" ", $TEXTL[$j]);
$cta=count($TEXTA);
//if ($cta==1) $TEXTA[0]=' '.$TEXTA[0].' ';
for ($i=0;$i<$cta;$i++)
{
if(strlen(trim($TEXTA[$i]))==0) continue;
$TEXTA[$i]=str_replace( 'i?', 'ı?',$TEXTA[$i]);//i8
//echo $TEXTA[$i].' ';
$sfile=bin2hex($TEXTA[$i]);
$spdir=substr($sfile,0,2);
$filename=$BASEDIR.$spdir.'/'.$sfile.'.png';
/***** Process PNG GRAPH *****/
if (!is_dir("{$BASEDIR}{$spdir}")) mkdir("{$BASEDIR}{$spdir}",0755);
//if (!file_exists($filename)) //always redo
{
$FSIZE=imagettfbbox($fontsize,0,$fontfile,$TEXTA[$i]);
$GX=$FSIZE[2]-$FSIZE[0]+2;
$GY=$FSIZE[3]-$FSIZE[5];
$YOFFSET=$GY/2;
$GY=$GY+$YOFFSET;
$pif1=ImageCreate($GX,$GY);
$BGCOLOR=ImageColorAllocate($pif1,255,255,255);//background
$trans=imagecolortransparent($pif1,$BGCOLOR);
$text_col=ImageColorAllocate($pif1,0,0,0x00);//fontcolor
imagefill($pif1,0,0,$BGCOLOR); //config background color
ImageTTFText($pif1,$fontsize,0,0,$GY-$YOFFSET,$text_col,$fontfile,$TEXTA[$i]);
ImagePNG($pif1,$filename);
ImageDestroy($pif1);
}
$restxt=$restxt."<img align=\"middle\" src=\"$filename\" alt=\"orig\" />";
} //for i
if ($j<(count($TEXTL)-1))
$restxt=$restxt.'<br />';
} //for j
return $restxt;
}
?>-------------------------------------------------------------------------------------
---Following is my asp.net c# code example but I only could transfer one character -----------------------
public string dfont(string message, string fontfile, string FilePath)
{
string restxt = "";
char[] deliminite = { '\r', '\n' };
string[] TEXTL = message.Split(deliminite);A: for (int j = 0; j < TEXTL.Length; j++)
{
if (TEXTL[j].Trim().Length == 0)
{
goto A;
}string[] TEXTA = TEXTL[j].Split(' ');
int cta = TEXTA.Length;
for (int k = 0; k < cta; k++)
{
if (TEXTA[k].Trim().Length == 0)
{
goto A;
}int i = 0;
Font f1 = new Font("Arial", 12);
int len = TEXTA[k].Length;
string str = TEXTA[k];
while (str.Length > 0)
{
string c = str.Substring(0, 1);
string CForder = CharToHex(c); //
bool DirectoryCheck = System.IO.Directory.Exists(FilePath + "\\" + CForder);
// Size textSize = TextRenderer.MeasureText(c, "Arial");if (!DirectoryCheck)
{
System.IO.Directory.CreateDirectory(FilePath + "\\" + CForder);
// Bitmap bmp = new Bitmap(14, textSize);
// Bitmap bmp = new Bitmap(14, 20);
Bitmap bmp = new Bitmap(20, 25);
Graphics g = Graphics.FromImage(bmp);
// g.FillRectangle(Brushes.LightBlue, 0, 0, 20, 25);
g.FillRectangle(Brushes.LightBlue, 0, 0, 20, 25);
PointF p1 = new PointF(0, 5);
g.DrawString(c, f1, Brushes.Blue, p1);
string filename = CForder + ".jpg";
string filePathname = FilePath + "\\" + CForder + "\\" + filename;
bmp.Save(filePathname, ImageFormat.Jpeg);
restxt += "<img class=content style='vertical-align:text-bottom' border=0 src=" + "mfont\\" + CForder + "\\" + filename + ">";
}
else
{string filename = CForder + ".jpg";
string filePathname = FilePath + "\\" + CForder + "\\" + filename;
restxt += "<img class=content style='vertical-align:text-bottom' border=0 src=" + "mfont\\" + CForder + "\\" + filename + ">";
}
// restxt += "<img src=ShowPicture.aspx?ImgSessionID=Pic" + i.ToString() + ">";
// this.FindControl("Form1").Controls.Add(new LiteralControl("<img src=ShowPicture.aspx?ImgSessionID=Pic" + i.ToString() + ">"));
str = str.Substring(1, str.Length - 1);
i += 1;
}
}
}return restxt;
}---------------------------------------------------------------------------------------------------------------------------------------------
Any suggestion is very appreciated!
Regards
Monday, June 16, 2008 3:36 AM
Answers
-
User537870505 posted
Check this:
http://ewbi.blogs.com/develops/2005/11/aspnet_dynamic_.html
http://www.codeproject.com/KB/web-image/createTextImage.aspx
http://sniptools.com/tutorials/creating-dynamic-images-in-aspnet (this also has additional links to similar tutorials)
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, June 16, 2008 4:16 AM -
User-967720686 posted
Hi,
Please run the code below in VS.Net. I hope this is something you want.
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head><!-- HTML -->
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TxtImageText" runat="server" TextMode="multiline" Rows="5" Columns="50"></asp:TextBox>
<br />
<asp:Literal ID="LtrImage" runat="server"></asp:Literal>
<br />
<asp:Button ID="BtnGetImage" Text=" DrawI mage " runat="server" OnClick="BtnGetImage_Click"></asp:Button>
</div>
</form>
</body>
</html>// Code behind
protected void Page_Load(object sender, EventArgs e)
{}
protected void BtnGetImage_Click(object sender, EventArgs e)
{
DrawImageFromText(TxtImageText.Text, "Images");
}/*
Namespaces Used
- using System.Drawing.Imaging;
- using System.Drawing.Drawing2D;
- using System.Drawing;
*/
private void DrawImageFromText(string text, string path)
{
string Font = "Arial";
float FontSize = 20;
int ImgHeight = 1;
int ImgWidth = 1;
int i = 0;char[] Sep = { '\r', '\n' };
string[] Texts = text.Split(Sep);
LtrImage.Text = string.Empty;foreach (string value in Texts)
{
if (value.Length > 0)
{
// Measuring Height and width of Image
ImgHeight = 1;
ImgWidth = 1;
Bitmap Bmp = new Bitmap(ImgWidth, ImgHeight);
Graphics Grp = Graphics.FromImage(Bmp);Font Fnt = new Font(Font, FontSize);
SizeF Size = Grp.MeasureString(value, Fnt);
ImgHeight = Convert.ToInt32(Size.Height) + 5;
ImgWidth = Convert.ToInt32(Size.Width) + 10;Bmp.Dispose();
Grp.Dispose();
Bmp = new Bitmap(ImgWidth, ImgHeight);
Grp = Graphics.FromImage(Bmp);
Grp.Clear(Color.LightGray);
Grp.DrawString(value, Fnt, Brushes.Blue, 5, 2);
string File = path + "/Image_" + i.ToString() + ".jpg";
Bmp.Save(Server.MapPath(File), ImageFormat.Jpeg);LtrImage.Text += "<img src=" + File + "/> <br>";
Bmp.Dispose();
Grp.Dispose();
i++;
}
}
}- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, June 20, 2008 3:41 AM
All replies
-
User537870505 posted
Check this:
http://ewbi.blogs.com/develops/2005/11/aspnet_dynamic_.html
http://www.codeproject.com/KB/web-image/createTextImage.aspx
http://sniptools.com/tutorials/creating-dynamic-images-in-aspnet (this also has additional links to similar tutorials)
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, June 16, 2008 4:16 AM -
User-967720686 posted
Hi,
Please run the code below in VS.Net. I hope this is something you want.
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head><!-- HTML -->
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TxtImageText" runat="server" TextMode="multiline" Rows="5" Columns="50"></asp:TextBox>
<br />
<asp:Literal ID="LtrImage" runat="server"></asp:Literal>
<br />
<asp:Button ID="BtnGetImage" Text=" DrawI mage " runat="server" OnClick="BtnGetImage_Click"></asp:Button>
</div>
</form>
</body>
</html>// Code behind
protected void Page_Load(object sender, EventArgs e)
{}
protected void BtnGetImage_Click(object sender, EventArgs e)
{
DrawImageFromText(TxtImageText.Text, "Images");
}/*
Namespaces Used
- using System.Drawing.Imaging;
- using System.Drawing.Drawing2D;
- using System.Drawing;
*/
private void DrawImageFromText(string text, string path)
{
string Font = "Arial";
float FontSize = 20;
int ImgHeight = 1;
int ImgWidth = 1;
int i = 0;char[] Sep = { '\r', '\n' };
string[] Texts = text.Split(Sep);
LtrImage.Text = string.Empty;foreach (string value in Texts)
{
if (value.Length > 0)
{
// Measuring Height and width of Image
ImgHeight = 1;
ImgWidth = 1;
Bitmap Bmp = new Bitmap(ImgWidth, ImgHeight);
Graphics Grp = Graphics.FromImage(Bmp);Font Fnt = new Font(Font, FontSize);
SizeF Size = Grp.MeasureString(value, Fnt);
ImgHeight = Convert.ToInt32(Size.Height) + 5;
ImgWidth = Convert.ToInt32(Size.Width) + 10;Bmp.Dispose();
Grp.Dispose();
Bmp = new Bitmap(ImgWidth, ImgHeight);
Grp = Graphics.FromImage(Bmp);
Grp.Clear(Color.LightGray);
Grp.DrawString(value, Fnt, Brushes.Blue, 5, 2);
string File = path + "/Image_" + i.ToString() + ".jpg";
Bmp.Save(Server.MapPath(File), ImageFormat.Jpeg);LtrImage.Text += "<img src=" + File + "/> <br>";
Bmp.Dispose();
Grp.Dispose();
i++;
}
}
}- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, June 20, 2008 3:41 AM