webForumDet fria alternativet

skala ett Graphics-objekt.

.NET

3 svar · 247 visningar · startad av icaaq

Medlem sedan okt. 20005 273 inlägg
Frågan#1

Hej.
Jag skapar en bild av denna kod.

[red]
PrivateFontCollection privateFont = new PrivateFontCollection();
privateFont.AddFontFile(@"F:\Inetpub\wwwroot\font\GOTHIC.TTF");
FontFamily fontFamily = privateFont.Families[0];
Font font = new Font(fontFamily, 32, (FontStyle.Regular) );

Bitmap objBitmap = new Bitmap(300, 100, PixelFormat.Format24bppRgb);
objBitmap.SetResolution(72, 72);
int width = objBitmap.Width, height = objBitmap.Height;

StringFormat stringFormat = new StringFormat();
stringFormat.Alignment = StringAlignment.Center;
stringFormat.LineAlignment = StringAlignment.Center;

Graphics objGraphics = Graphics.FromImage(objBitmap);
objGraphics.SmoothingMode = SmoothingMode.AntiAlias;
objGraphics.TextRenderingHint = TextRenderingHint.AntiAlias;
objGraphics.Clear(Color.White);
objGraphics.DrawRectangle(Pens.AliceBlue, 3, 3, width-1, height-1);
objGraphics.DrawString(Convert.ToString(HttpContext.Current.Request.QueryString["text"]), font, new SolidBrush(Color.Black), 
	new Rectangle(0, 0, width, height), stringFormat);

[b]
int newWidth, newHeight;
double ImageScale = (double)300 / (double)objBitmap.Width;
newWidth = Convert.ToInt32(objBitmap.Width * ImageScale);
newHeight = Convert.ToInt32(objBitmap.Height * ImageScale);	

objGraphics.DrawImage(objBitmap ,new Rectangle(100, 0, newWidth, newHeight));[/b]

MemoryStream ms = new MemoryStream();
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();
}
[/red]

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å :)

mv icaaq

Medlem sedan dec. 19991 072 inlägg
#2

Till att börja med....Vad blir fel nu?

Medlem sedan okt. 20005 273 inlägg
#3

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]

mv icaaq

Medlem sedan okt. 20005 273 inlägg
#4

Ni kan se ett exempel här
<http://www.19december.se/textimage.aspx?text=wF Rockar&width=500>

mv icaaq

264 ms totalt · 4 externa anrop · v20260731065814-full.a51de22e
128 ms — deklarationer (db)
0 ms — hämta statistik (cache)
133 ms — hämta tråd, inlägg och bilagor (db)
121 ms — ändringar (db)