|
If you use GDI+ and try to display some gifs image dynamically from SQL for instance, you will confronted by the poor quality of the rendered picture.
This class will produce a much better result. Pass a stream to the class constructor.
using System;
using System.Drawing;
using System.Web;
using System.IO;
using System.Text;
using System.Drawing.Text;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
namespace DynamicImageGeneration
{
///
/// Summary description for WebForm1.
///
public class WebForm1 : System.Web.UI.Page
{
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
Bitmap bitmap = new Bitmap(Server.MapPath("sanjeeb.bmp"));
MemoryStream memStream = new MemoryStream();
// Generate Image
// Create a Graphics Object for drawing.
Graphics g = Graphics.FromImage(bitmap);
g.SmoothingMode = SmoothingMode.AntiAlias;
int width = bitmap.Width;
int height = bitmap.Height;
string familyName = "Tahoma";
// Put the text here
string text = "Sanjeeb Kumar Sarangi";
// Get a rectangle on the shirt
Rectangle rect = new Rectangle(20, 216, 210, 135);
// Set up the text font.
Font font;
font = new Font(familyName, 16F, FontStyle.Regular);
// Set up the text format.
StringFormat format = new StringFormat();
format.Alignment = StringAlignment.Center;
format.LineAlignment = StringAlignment.Center;
// Create a path using the text and warp it to fit over the contour
GraphicsPath path = new GraphicsPath();
path.AddString(text, font.FontFamily, (int) font.Style, font.Size, rect, format);
PointF[] points =
{
new PointF(rect.X - 10, rect.Y - 8),
new PointF(rect.X + rect.Width - 20, rect.Y + 4),
new PointF(rect.X - 8, rect.Y + rect.Height - 15),
new PointF(rect.X + rect.Width - 10, rect.Y + rect.Height + 4)
};
Matrix matrix = new Matrix();
matrix.Translate(0F, 0F);
path.Warp(points, rect, matrix, WarpMode.Perspective, 0F);
// Draw the text.
HatchBrush hatchBrush = new HatchBrush(
HatchStyle.LargeConfetti,
Color.LightGray,
Color.DarkGray);
g.FillPath(hatchBrush, path);
Response.Clear();
Response.ContentType="image/jpeg";
bitmap.Save(memStream, ImageFormat.Jpeg);
memStream.WriteTo(Response.OutputStream);
// Clean up.
font.Dispose();
hatchBrush.Dispose();
g.Dispose();
bitmap.Dispose();
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}
|