Skip to main content

how to resize image and show resized image

step 1: Take a image button and assign ImageUrl is equal to original Image Path
Eg :- Image.ImageUrl = "image/originalImage.jpeg"

Step 2: Then On pageLoad write the code:-
Image.ImageUrl = "view.aspx?imgUrl =" + Image.ImageUrl + "&Width=100&Height=100";

step 3: write the Following Coding on the view.aspx Page:-


private void Page_Load(object sender, System.EventArgs e)
{
string url="../Products/image/imageNot.jpg";
int Weight=60,Height=60;
if(Request.QueryString["URL"]!=null)
{
url = Request.QueryString["URL"].ToString();
Weight = Convert.ToInt32(Request.QueryString["Width"].ToString());
Height = Convert.ToInt32(Request.QueryString["Height"].ToString());
}
Resize(url,Weight,Height);
}

public void Resize(string strPath, int Weight, int Height)
{
string strFile = strPath.Substring(strPath.LastIndexOf("/")+1);
string urlPhysical = HttpContext.Current.Server.MapPath(strPath);
if(!File.Exists(urlPhysical))
{
urlPhysical = HttpContext.Current.Server.MapPath("../images/imageNot.jpg");
}
Bitmap btm = ResizeImage(urlPhysical,Weight,Height);
ImageConverter ic = new ImageConverter();
byte[] btImage1 = new byte[1];
btImage1 = (byte[])ic.ConvertTo(btm, btImage1.GetType());
Response.BinaryWrite(btImage1);
}

public Bitmap ResizeImage(string url, int intH,int intW)
{
Bitmap objImg = new Bitmap(url);
Bitmap outputImage = new Bitmap(intW, intH, objImg.PixelFormat);
outputImage.SetResolution(objImg.HorizontalResolution, objImg.VerticalResolution);
Graphics graphics = Graphics.FromImage(outputImage);
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphics.DrawImage(objImg, new Rectangle(0, 0, intW, intH),new Rectangle(4, 4, objImg.Width-4, objImg.Height-4), GraphicsUnit.Pixel);
graphics.Dispose();
return outputImage;
}

Comments

Popular posts from this blog

IIS, There was an error when trying to connect. Do you want to retype your credentials and try again?

IIS 8 error on windows server 2012 insufficient permission There was an error when trying to connect. Do you want to retype your credentials and try again? Details: Filename: \?\C:Windows\system32\inetsrv\config\redirection.config Error: Cannot read configuration file due to insufficient permissions screenshot: Solution: Steps to short out this issue: go to C:\Windows\Microsoft.Net\Framework64\v2.0.50727\CONFIG\machine.config here you got the redirection tag like the below <configurationredirection enabled="true" password="[enc:IISCngProvider:X0ObCWwZ4+PrTHiFVPtzFeCcL8u5P6KUOYfo1/0QrgZWATA5pKWqHvD8nL2crNJKyyqr4z/rBdLPjdRcaLxAMMj4l+lvp5EXXKSXueolvyGa34F4QZfbBVCM6oVNcq3M368TOTVjJv4POVFQWvu0MDVlGgReglXB+Lw5BRI4Htw=:enc]" path="C:\Windows\System32\inetsrv\config\import\" username="Administrator"> </configurationredirection"></li"> you need to change this to <configurationRedirection /> Th...

Plesk - Upgrade the .net Framework to 4.7.2 OR 4.8

 Steps to Upgrade the .net Framework to 4.7.2 OR 4.8 1. First Download the .NET Framework from https://dotnet.microsoft.com/download/dotnet-framework   2. Install on the Server. 3. Login to the Plesk and go to "Tools & Settings" > "Server Components" and refresh the components using the refresh button 4. After update, it will reflect in "Web Script" section at the server component page. All done!!!