---
title: "skala ett Graphics-objekt."
type: "forum-thread"
url: "https://www.webforum.nu/amne/dotnet/100059-skala-ett-graphics-objekt"
topic: ".NET"
topic_url: "https://www.webforum.nu/amne/dotnet"
author: "icaaq"
published: "2004-03-22T12:31:10.000Z"
updated: "2004-03-22T15:25:30.000Z"
replies: 3
views: 250
page: 1
pages: 1
language: "sv-SE"
site: "webForum — webforum.nu"
rights: "Upphovsrätten till varje inlägg tillhör dess författare."
attribution: "Citera som: webForum, https://www.webforum.nu/amne/dotnet/100059-skala-ett-graphics-objekt"
---

# skala ett Graphics-objekt.

## #1 — icaaq, 2004-03-22T12:31Z

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

Permalänk: https://www.webforum.nu/p/100059

## #2 — fredrik, 2004-03-22T15:08Z

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

Permalänk: https://www.webforum.nu/p/1305137

## #3 — icaaq, 2004-03-22T15:21Z

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

Permalänk: https://www.webforum.nu/p/1305152

## #4 — icaaq, 2004-03-22T15:25Z

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

mv icaaq

Permalänk: https://www.webforum.nu/p/1305156

---

Tråden på webben: https://www.webforum.nu/amne/dotnet/100059-skala-ett-graphics-objekt
