PDA

View Full Version : آموزش: ساختن تصویر در سی شارپ



ایمان مدائنی
یک شنبه 29 اردیبهشت 1392, 22:13 عصر
سلام دوستان

با یک آموزش دیگه اومدم

اینبار میخوام بهتون یاد بدم با استفاده از سی شارپ تصویر بسازید
http://madaeny.com/ImagesArticle/25cc5599dc25406d9bc2f7b8b4c8dc6c.jpg
نمونه برنامه را ضمیمه کردم فقط قسمت های مهم رو توضیح میدم

بریم سروقت کلید تصویر را بساز


string strUrl = "pagecounter.aspx?";
strUrl += "HitFontName=" + Server.UrlEncode(fontSelection.SelectedItem.Text);
strUrl += "&HitFontSize=" + txtFontSize.Text;
strUrl += "&HitCount=" + txtHitValue.Text;
strUrl += "&HitBackgroundColor=" + Server.UrlEncode("#" + bgclrSelection.SelectedItem.Value);
strUrl += "&HitFontColor=" + Server.UrlEncode("#" + fontclrSelection.SelectedItem.Value);
PageCounter.ImageUrl = strUrl;

خب شاید کدها کمی ترسناک باشه ولی در واقع کلک یا ترفند جالبی است

pagecounter.aspx در واقع یک صفحه جداگانه می باشد که کار آن ساختن تصویر و برگرداندن آن بدون ذخیره کردن است

اینکار برای اینه که تصاویر ساخته شده جایی ذخیره نشوند مثال تصاویر امنیتی

البته قسمت ذخیره هم داره که من به صورت کامنت در آوردم

همانطور که در کد میبینید پارامتر های به صفحه مربوطه ارسال شدند

بزارید کد صفحه ساختن تصویر رو هم ببینیم



Response.Expires = 0;
Bitmap newBitmap = null;
Graphics g = null ;
string str2Render = Request.QueryString.Get("HitCount");
if (null == str2Render) str2Render = "no count specified";
string strFont = Request.QueryString.Get("HitFontName");
if (null == strFont) strFont = "Lucida Sans Unicode";
int nFontSize = 12;
try
{
nFontSize = Int32.Parse(Request.QueryString.Get("HitFontSize"));
}
catch
{
// do nothing, just ignore
}
string strBackgroundColorname = Request.QueryString.Get("HitBackgroundColor");
Color clrBackground = Color.White;
try
{
// Format in the URL: %23xxXXxx
if (null != strBackgroundColorname)
clrBackground = ColorTranslator.FromHtml(strBackgroundColorname);
}
catch
{
}
string strFontColorName = Request.QueryString.Get("HitFontColor");
Color clrFont = Color.Black;
try
{
// Format in the URL: %23xxXXxx
if (null != strFontColorName)
clrFont = ColorTranslator.FromHtml(strFontColorName);
}
catch
{
}
try
{
Font fontCounter = new Font(strFont, nFontSize);
// calculate size of the string.
newBitmap = new Bitmap(1,1,PixelFormat.Format32bppArgb);
g = Graphics.FromImage(newBitmap);
SizeF stringSize = g.MeasureString(str2Render, fontCounter);
int nWidth = (int)stringSize.Width;
int nHeight = (int)stringSize.Height;
g.Dispose();
newBitmap.Dispose();

newBitmap = new Bitmap(nWidth,nHeight,PixelFormat.Format32bppArgb) ;
g = Graphics.FromImage(newBitmap);
g.FillRectangle(new SolidBrush(clrBackground), new Rectangle(0,0,nWidth,nHeight));
g.DrawString(str2Render, fontCounter, new SolidBrush(clrFont), 0, 0);

MemoryStream tempStream = new MemoryStream();
newBitmap.Save(tempStream,ImageFormat.Png);

Response.ClearContent();
Response.ContentType = "image/png";
Response.BinaryWrite(tempStream.ToArray());
Response.End();
// newBitmap.Save(Response.OutputStream, ImageFormat.Png);
// newBitmap.Save("o:\\TestApps\\TestServer\\test.png", ImageFormat.Png) ;
}
catch (Exception e)
{
Response.Write(e.ToString());
}
finally
{
if (null != g) g.Dispose();
if (null != newBitmap) newBitmap.Dispose();
}

همانطور که میبینید این تکه کد پارامتر هارو دریافت کرده و شروع به ساختن تصویر از روی پارامتر های فوق میکند

سپس یک img روی صفحه است که تصویر ساخته شده را تحویل میگیرد


<asp:Image id="PageCounter" runat="server" imageurl="pagecounter.aspx" />

حالا جالب تر اینه که وقتی با فایرباگ روی تصویر میری آدرس رو ببینید چطور نشون میده



<img id="PageCounter" src="pagecounter.aspx?HitFontName=Times+New+Roman&HitFontSize=32&HitCount=ایمان مدائنی&HitBackgroundColor=%23ff0000&HitFontColor=%2300ff00" mce_src="pagecounter.aspx?HitFontName=Times+New+Roman&HitFontSize=32&HitCount=ایمان مدائنی&HitBackgroundColor=%23ff0000&HitFontColor=%2300ff00">


نمونه برنامه را ضمیمه کردم

امیدوارم خوشتون اومده باشه



موفق و پیروز باشید

منبع مقاله : Madaeny.com (http://www.madaeny.com/Article/535/)