Re-size image with Transparency effect using asp.net with VB and ASP.NET(C#)
VB:
Here SaveImage is the method for Re-size the image with Transparancy...... Public Shared Sub SaveImage(imgheight As Integer, imgwidth As Integer, imgfilename As String, path As String, upImage As FileUpload) Dim width As Integer = imgwidth Dim height As Integer = imgheight Dim fileName As String = imgfilename Dim redPercentage As Single = 100 Dim bmpOrg As New System.Drawing.Bitmap(upImage.PostedFile.InputStream) Dim wPercentage As Single = (Convert.ToSingle(width) * 100) / Convert.ToSingle(bmpOrg.Width) Dim hPercentage As Single = (Convert.ToSingle(height) * 100) / Convert.ToSingle(bmpOrg.Height) redPercentage = If((wPercentage < hPercentage), wPercentage, hPercentage) redPercentage = If((redPercentage > 100), 100, redPercentage) Dim imgBanner As System.Drawing.Image = ImageResize.ScaleByPercent(bmpOrg, redPercentage) Dim bmpOrg1 As New System.Drawing.Bitmap(imgBanner) Dim newGraphic As Graphics = Graphics.FromImage(bmpOrg1) newGraphic.Clear(Color.Transparent) newGraphic.DrawImage(bmpOrg, 0, 0, bmpOrg1.Width, bmpOrg1.Height) bmpOrg1.MakeTransparent(bmpOrg1.GetPixel(0, 0)) If Not Directory.Exists(path) Then Directory.CreateDirectory(path) End If bmpOrg1.Save(path & fileName) End Sub In above code we have one more function.... ImageResize.ScaleByPercent(bmpOrg, redPercentage) this menthod using to Resize the image with Ration. Create a class file name with ImageResize and write this function in that class. Public Shared Function ScaleByPercent(ImgPicture As System.Drawing.Image, percent As Single, tWidth As Integer, tHeight As Integer) As System.Drawing.Image Dim nPercent As Single = (CSng(percent) / 100) Dim sourceWidth As Integer = ImgPicture.Width Dim sourceHeight As Integer = ImgPicture.Height Dim sourceX As Integer = 0 Dim sourceY As Integer = 0 Dim destWidth As Integer = CInt(sourceWidth * nPercent) Dim destHeight As Integer = CInt(sourceHeight * nPercent) Dim destX As Integer = (tWidth - destWidth) / 2 Dim destY As Integer = (tHeight - destHeight) / 2 Dim bmPicture As New Bitmap(tWidth, tHeight, PixelFormat.Format24bppRgb) bmPicture.SetResolution(ImgPicture.HorizontalResolution, ImgPicture.VerticalResolution) Dim grPicture As Graphics = Graphics.FromImage(bmPicture) grPicture.InterpolationMode = InterpolationMode.HighQualityBicubic grPicture.Clear(Color.White) grPicture.DrawImage(ImgPicture, New Rectangle(destX, destY, destWidth, destHeight), New Rectangle(sourceX, sourceY, sourceWidth, sourceHeight), GraphicsUnit.Pixel) grPicture.Dispose() Return bmPicture End Function
C#:
asp.net with C# user can use this code for Re-size image with Transparancy.... public static void SaveImage(int imgheight, int imgwidth, string imgfilename, string path, FileUpload upImage) { int width = imgwidth; int height = imgheight; string fileName = imgfilename; float redPercentage = 100; try { var bmpOrg = new Bitmap(upImage.PostedFile.InputStream); float wPercentage = (Convert.ToSingle(width) * 100) / Convert.ToSingle(bmpOrg.Width); float hPercentage = (Convert.ToSingle(height) * 100) / Convert.ToSingle(bmpOrg.Height); redPercentage = (wPercentage < hPercentage) ? wPercentage : hPercentage; redPercentage = (redPercentage > 100) ? 100 : redPercentage; bmpOrg.MakeTransparent(); //to save image with new height and width without frame var imgBanner = ImageResize.ScaleByPercent(bmpOrg, redPercentage); //to save image with new height and width with frame specified //System.Drawing.Image imgBanner = ImageResize.ScaleByPercent(bmpOrg, redPercentage, width, height); System.Drawing.Bitmap bmpOrg1 = new System.Drawing.Bitmap(imgBanner); Graphics newGraphic = Graphics.FromImage(bmpOrg1); newGraphic.Clear(Color.Transparent); newGraphic.DrawImage(bmpOrg, 0, 0, bmpOrg1.Width, bmpOrg1.Height); bmpOrg1.MakeTransparent(bmpOrg1.GetPixel(0, 0)); if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } bmpOrg1.Save(path + fileName); } catch (Exception ex) { throw ex; } } In above code we have one more function.... ImageResize.ScaleByPercent(bmpOrg, redPercentage, width, height); public static System.Drawing.Image ScaleByPercent(System.Drawing.Image imgPhoto, float Percent) { float nPercent = ((float)Percent / 100); int sourceWidth = imgPhoto.Width; int sourceHeight = imgPhoto.Height; int sourceX = 0; int sourceY = 0; int destX = 0; int destY = 0; int destWidth = (int)(sourceWidth * nPercent); int destHeight = (int)(sourceHeight * nPercent); Bitmap bmPhoto = new Bitmap(destWidth, destHeight, PixelFormat.Format24bppRgb); bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution); Graphics grPhoto = Graphics.FromImage(bmPhoto); grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic; grPhoto.Clear(Color.White); grPhoto.DrawImage(imgPhoto, new Rectangle(destX, destY, destWidth, destHeight), new Rectangle(sourceX, sourceY, sourceWidth, sourceHeight), GraphicsUnit.Pixel); grPhoto.Dispose(); return bmPhoto; }