Nu skulle jag vilja ha lite förslag på hur jag kan skala om bilden efter att jag lagt dit texten. Det är alltså den feta texten som inte fungerar, och som jag vill få exempel på :)
Felet var att jag inte kunde skala bilden. Detta löste jag på detta vis.
[red]
using System;
using System.Web;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.Drawing.Text;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using NineteenDecember;
namespace NineteenDecember.HttpHandler
{
/// <summary>
/// Summary description for viewimage.
/// </summary>
public class GenerateTextImage: IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
// Sets the Culture to Swedish
CultureInfo info = new CultureInfo("sv-SE", false);
// Create a memoryStream, more on that later
MemoryStream ms = new MemoryStream();
// Sets the orginal size of the bitmap
int width = 230;
int height = 60;
// Creates the original bitmap
Bitmap objBitmap = new Bitmap(width, height, PixelFormat.Format24bppRgb);
objBitmap.SetResolution(72, 72);
// Sets the default font
string fontName = "Arial.TTF";
// Checks if a specified font is posted
if(HttpContext.Current.Request.QueryString["fontName"] != null)
fontName = Convert.ToString(HttpContext.Current.Request.QueryString["fontName"]);
// Creates a PrivateFontCollection
PrivateFontCollection privateFont = new PrivateFontCollection();
// Add the fontFile to the collection and creates a new font object.
privateFont.AddFontFile(HttpContext.Current.Server.MapPath("/font")+"/"+fontName);
FontFamily fontFamily = privateFont.Families[0];
Font font = new Font(fontFamily, 32, (FontStyle.Regular));
// Center the text
StringFormat stringFormat = new StringFormat();
stringFormat.Alignment = StringAlignment.Center;
stringFormat.LineAlignment = StringAlignment.Center;
// Create the Grapichs
Graphics objGraphics = Graphics.FromImage(objBitmap);
objGraphics.SmoothingMode = SmoothingMode.AntiAlias;
objGraphics.TextRenderingHint = TextRenderingHint.AntiAlias;
objGraphics.Clear(Color.White);
// Write the Text
objGraphics.DrawString(Convert.ToString(HttpContext.Current.Request.QueryString["text"], info), font, new SolidBrush(Color.Black),
new Rectangle(0, 0, width, height), stringFormat);
[b] if(HttpContext.Current.Request.QueryString["width"] != null)
{
// Calculate the scale
double ImageScale = Convert.ToDouble(HttpContext.Current.Request.QueryString["width"]) / (double)objBitmap.Width;
int newWidth = Convert.ToInt32(objBitmap.Width * ImageScale);
int newHeight = Convert.ToInt32(objBitmap.Height * ImageScale);
// Create a new bitmap with the scaled size
Bitmap final = null;
final = new Bitmap(newWidth, newHeight);
// Create the Grapichs for the scaled version
Graphics g = Graphics.FromImage(final);
g.CompositingMode = CompositingMode.SourceCopy;
g.Clear(Color.White);
g.InterpolationMode = InterpolationMode.High;
g.DrawImage(objBitmap,new Rectangle(0, 0, newWidth, newHeight),new Rectangle(0, 0, width, height), GraphicsUnit.Pixel);
// Saves the scaled version to the memoryStream
final.Save(ms, ImageFormat.Jpeg);
}[/b]
else
{
objBitmap.Save(ms, ImageFormat.Jpeg);
}
Byte[] Buffer = ms.ToArray();
HttpContext.Current.Response.BufferOutput = true;
try
{
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.ClearHeaders();
HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
HttpContext.Current.Response.ContentType = "Image/jpeg";
HttpContext.Current.Response.AppendHeader("content-disposition", "inline");
HttpContext.Current.Response.OutputStream.Write(Buffer, 0, Buffer.Length);
}
catch( Exception ex )
{
//Debug.WriteLine(inputFileName);
throw(ex);
}
objGraphics.Dispose();
objBitmap.Dispose();
}
public bool IsReusable
{
get
{
return true;
}
}
}
}
[/red]