Re-size image with Transparency effect using asp.net with VB and ASP.NET(C#)

图片透明缩放
本文介绍了一种使用VB和C#实现的图片缩放方法,该方法可在保持图片透明度的同时按比例调整图片大小。通过计算缩放百分比并利用自定义的ScaleByPercent函数,可以有效地调整图片尺寸。

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;

}

 

转载于:https://www.cnblogs.com/happy-Chen/p/3639941.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值