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.
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 |
using System; using System.Drawing; using System.Drawing.Imaging; using System.Drawing.Drawing2D; using System.IO; using System.Web; using System.Web.UI; public partial class thumbnail : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { string image = Request.QueryString["Image"]; if (string.IsNullOrEmpty(image)) { this.ErrorResult(); return; } string sSize = Request.QueryString["Size"]; int size = 313; if (!string.IsNullOrEmpty(sSize) && !int.TryParse(sSize, out size)) { this.ErrorResult(); return; } string path; if (image.IndexOf("sliders", StringComparison.OrdinalIgnoreCase) == -1) { path = Server.MapPath("/uploads/eticaret/" + image); } else { path = Server.MapPath("/uploads/slider/" + image.Split('_')[1]); } if (!File.Exists(path)) { this.ErrorResult(); return; } Bitmap bmp; try { bmp = CreateThumbnails(path, size); } catch { this.ErrorResult(); return; } if (bmp == null) { this.ErrorResult(); return; } string imagePath = Server.MapPath("/uploads/" + size + "_" + image); Response.ContentType = "image/jpeg"; try { if (File.Exists(imagePath)) { Response.WriteFile(imagePath); } else { using (MemoryStream ms = new MemoryStream()) { bmp.Save(ms, ImageFormat.Jpeg); ms.WriteTo(Response.OutputStream); bmp.Save(imagePath, ImageFormat.Jpeg); } } } catch { this.ErrorResult(); } finally { bmp.Dispose(); } } private void ErrorResult() { Response.Clear(); Response.StatusCode = 404; Response.End(); } public static Bitmap CreateThumbnails(string lcFilename, int targetSize) { Bitmap bmpOut = new Bitmap(targetSize, targetSize); try { using (Bitmap originalBmp = new Bitmap(lcFilename)) { float ratio = Math.Min((float)targetSize / originalBmp.Width, (float)targetSize / originalBmp.Height); int newWidth = (int)(originalBmp.Width * ratio); int newHeight = (int)(originalBmp.Height * ratio); using (Graphics g = Graphics.FromImage(bmpOut)) { g.CompositingQuality = CompositingQuality.HighQuality; g.SmoothingMode = SmoothingMode.HighQuality; g.InterpolationMode = InterpolationMode.HighQualityBicubic; g.PixelOffsetMode = PixelOffsetMode.HighQuality; g.Clear(Color.White); int posX = (targetSize - newWidth) / 2; int posY = (targetSize - newHeight) / 2; g.DrawImage(originalBmp, posX, posY, newWidth, newHeight); } } } catch { bmpOut.Dispose(); 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 yanıt yazın