Hej,Skapa bilder från text
Är absolut newbie på C# :) och håller på att testa lite - framförallt grafiken ...
Bifogar kod jag hittat och leker med. Mina tre främsta frågor är:
1. Hur byter jag t.ex. till .png format (har försökt men får error)?
2. Hur får jag koden att spara bilden som en fil (pic.Save --> till fil istället för output stream)?
3. Var hittar jag bra dokumentation om fontegenskaper och grafiken?
<%@ Page Language="C#" trace="false" Explicit="true" aspcompat="true" Debug="true" %>
<%@ Import Namespace="System" %>
<%@ Import Namespace="System.Text" %>
<%@ Import Namespace="System.Drawing" %>
<%@ Import Namespace="System.Drawing.Imaging" %>
<%@ Import Namespace="System.Drawing.Text" %>
<%@ Import Namespace="System.Drawing.Drawing2D" %>
<script runat="server">
//-------------------------------------
// fonter.net v1.0
//-------------------------------------
// Text Images On the Fly using ASP.Net
// Written in C# and GDI+ library
//-------------------------------------
// (C) Zeddy Iskandar, 2003-onwards.
// Provided as-is, author is not
// responsible for anything.
//-------------------------------------
public void Page_Load(object sender, System.EventArgs e)
{
if (Request.HttpMethod == "POST")
{
string text = "Hej detta är ett test";
int textLength = text.Length;
int fontSize = 24;
int orientation = 1;
int antialias = 1;
// Set canvas width & height
int width;
int height;
width = (fontSize * textLength) - ((textLength * fontSize)/3);
height = fontSize + 20;
// Initialize graphics
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 = "CornflowerBlue";
string bgColor = "Beige";
Color fontColor = Color.FromName(fgColor);
Color rectColor = Color.FromName(bgColor);
SolidBrush fgBrush = new SolidBrush(fontColor);
SolidBrush bgBrush = new SolidBrush(rectColor);
// Rectangle
//int bound = 1;
g.FillRectangle(bgBrush, rectF);
// Load font
string fontName = "Avalon";
string fontFile = "Avalonn.TTF";
PrivateFontCollection privateFontCollection = new PrivateFontCollection();
privateFontCollection.AddFontFile(Server.MapPath("./") + fontFile);
FontFamily fontFamily = privateFontCollection.Families[0];
// Set font style
FontStyle style = FontStyle.Regular;
// style = FontStyle.Bold;
// style = FontStyle.Italic;
// style = (FontStyle.Bold) | (FontStyle.Italic);
// style = FontStyle.Strikeout;
// style = FontStyle.Underline;
Font font = new Font(fontFamily, fontSize, style, GraphicsUnit.Pixel);
// Set font direction & alignment
StringFormat format = new StringFormat();
//int reverse = 1;
//format.FormatFlags = StringFormatFlags.DirectionRightToLeft;
format.Alignment = StringAlignment.Near;
// format.Alignment = StringAlignment.Center;
// format.Alignment = StringAlignment.Far;
format.LineAlignment = StringAlignment.Center;
// Draw any drop-shadow
int dropShadow = 0;
// Finally, draw the font
g.DrawString(text, font, fgBrush, rectF, format);
Response.ContentType = "image/jpeg";
pic.Save(Response.OutputStream, ImageFormat.Jpeg);
// Dispose objects
pic.Dispose();
}
else
{
Response.ContentType = "text/html";
Response.Write("<html><body>");
Response.Write("fonter.net v1.0<br>Create Text Images On-the-Fly");
Response.Write("(C)2003-onwards, Zeddy Iskandar");
Response.Write("</body></html>");
}
}
</script>