using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Drawing; using System.Drawing.Drawing2D; using System.Drawing.Imaging; public partial class **bildgalleri** : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { string WorkingDirectory = Server.MapPath("**/bilder/**"); Image imgPhotoVert = Image.FromFile(WorkingDirectory + "**DSC00029.JPG**"); Image imgPhoto = null; imgPhoto = FixedSize(imgPhotoVert, **150, 150**); _// önskad storlek på bilden_ imgPhoto.Save(WorkingDirectory + "**tumme_DSC00029.JPG**", ImageFormat.Jpeg); imgPhoto.Dispose(); } static Image FixedSize(Image imgPhoto, int Width, int Height) { int sourceWidth = imgPhoto.Width; int sourceHeight = imgPhoto.Height; int sourceX = 0; int sourceY = 0; int destX = 0; int destY = 0; float nPercent = 0; float nPercentW = 0; float nPercentH = 0; nPercentW = ((float)Width / (float)sourceWidth); nPercentH = ((float)Height / (float)sourceHeight); if (nPercentH < nPercentW) { nPercent = nPercentH; destX = System.Convert.ToInt16((Width - (sourceWidth \* nPercent)) / 2); } else { nPercent = nPercentW; destY = System.Convert.ToInt16((Height - (sourceHeight \* nPercent)) / 2); } int destWidth = (int)(sourceWidth \* nPercent); int destHeight = (int)(sourceHeight \* nPercent); Bitmap bmPhoto = new Bitmap(Width, Height, PixelFormat.Format24bppRgb); bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution); Graphics grPhoto = Graphics.FromImage(bmPhoto); grPhoto.Clear(Color.White); grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic; grPhoto.DrawImage(imgPhoto, new Rectangle(destX, destY, destWidth, destHeight), new Rectangle(sourceX, sourceY, sourceWidth, sourceHeight), GraphicsUnit.Pixel); grPhoto.Dispose(); return bmPhoto; } }
Skapa tumnaglar med C#
Här kommer ett kodexempel för hur man kan krympa storleken på bilder eller skapa tumnaglar.