Asp.net İle Dinamik Thumbnail Oluşturma
18 Mayıs 2017 / 11:52
Burak
Merhaba arkadaşlar, bugünkü kod kırıntılarında sizlere dinamik olarak thumbnail oluşturma kod pıtırcıklarını paylaşıcam.Kodlarla uğraşmak istemeyen direk burdan çekebilir kodlar ( hadi gene iyisiniz millet gibi uğraştırmıyorum sizi 🙂 )
Yok illa ben kodları görücem diyorsanız kaputun altı aşağıdaki gibidir.
Dosya isminin thumbnail.aspx olduğunu varsayarsak
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Drawing; using System.Drawing.Imaging; public partial class thumbnail : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { string Image = Request.QueryString["Image"]; if (Image == null) { this.ErrorResult(); return; } string sSize = Request["Size"]; int Size = 120; if (sSize != null) Size = Int32.Parse(sSize); string Path = Server.MapPath(Request.ApplicationPath) + "\\" + Image; Bitmap bmp = CreateThumbnails(Path, Size, Size); if (bmp == null) { this.ErrorResult(); return; } string OutputFilename = null; OutputFilename = Request.QueryString["OutputFilename"]; if (OutputFilename != null) { if (this.User.Identity.Name == "") { // *** Custom error display here bmp.Dispose(); this.ErrorResult(); } try { bmp.Save(OutputFilename); } catch (Exception ex) { bmp.Dispose(); this.ErrorResult(); return; } } // Put user code to initialize the page here Response.ContentType = "image/jpeg"; bmp.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg); bmp.Dispose(); } private void ErrorResult() { Response.Clear(); Response.StatusCode = 404; Response.End(); } /// /// Creates a resized bitmap from an existing image on disk. /// Call Dispose on the returned Bitmap object /// /// /// /// /// Bitmap or null public static Bitmap CreateThumbnails(string lcFilename, int lnWidth, int lnHeight) { System.Drawing.Bitmap bmpOut = null; try { Bitmap loBMP = new Bitmap(lcFilename); ImageFormat loFormat = loBMP.RawFormat; decimal lnRatio; int lnNewWidth = 0; int lnNewHeight = 0; //*** If the image is smaller than a thumbnail just return it if (loBMP.Width < lnWidth && loBMP.Height < lnHeight) return loBMP; if (loBMP.Width > loBMP.Height) { lnRatio = (decimal)lnWidth / loBMP.Width; lnNewWidth = lnWidth; decimal lnTemp = loBMP.Height * lnRatio; lnNewHeight = (int)lnTemp; } else { lnRatio = (decimal)lnHeight / loBMP.Height; lnNewHeight = lnHeight; decimal lnTemp = loBMP.Width * lnRatio; lnNewWidth = (int)lnTemp; } // System.Drawing.Image imgOut = // loBMP.GetThumbnailImage(lnNewWidth,lnNewHeight, // null,IntPtr.Zero); // *** This code creates cleaner (though bigger) thumbnails and properly // *** and handles GIF files better by generating a white background for // *** transparent images (as opposed to black) bmpOut = new Bitmap(lnNewWidth, lnNewHeight); Graphics g = Graphics.FromImage(bmpOut); g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; g.FillRectangle(Brushes.White, 0, 0, lnNewWidth, lnNewHeight); g.DrawImage(loBMP, 0, 0, lnNewWidth, lnNewHeight); loBMP.Dispose(); } catch { return null; } return bmpOut; } } |
Kullanımı ise aşağıdaki gibidir
http://www.domain.com/thumbnail.aspx?image=resim_yolu.jpg&size=resim_boyut
Herkese bol debuglu bol queryli günler 🙂
Bu Yazılarıda Okuyabilirsiniz...
Bir cevap yazın