For resizing the original image in ASP.net application, Follow the below steps,
Step-1:
Create an ASP.net application and add a webform in it. In design page, add FileUpload control for uploading an image, two textbox for width and height, two image control first one for to show real size of image and the second one for resized image and add a button for submit your details . Follow the source code in your design page...
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
.auto-style1 {
width: 100%;
}
.auto-style2 {
}
.auto-style3 {
width: 332px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<table class="auto-style1">
<tr>
<td class="auto-style2">upload an image :</td>
<td class="auto-style3">
<asp:FileUpload ID="FileUpload1" runat="server" />
</td>
<td class="auto-style3">Width:
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</td>
<td class="auto-style3">Height :<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
</td>
<td>
<asp:Button ID="Button1" runat="server" Text="Resize Image" OnClick="Button1_Click" />
</td>
</tr>
<tr>
<td class="auto-style2" colspan="5">Original :<br />
<asp:Image ID="Image1" runat="server" />
<br />
<br />
After Resizing:<br />
<asp:Image ID="Image2" runat="server" />
<br />
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
Step-2:
Write method for resize an image,
public static Bitmap ResizeImage(Stream stream, int hght, int wdth)
{
int height = hght;
int width = wdth;
Bitmap scaledImage = new Bitmap(width, height);
try
{
System.Drawing.Image originalImage = Bitmap.FromStream(stream);
using (Graphics g = Graphics.FromImage(scaledImage))
{
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.DrawImage(originalImage, 0, 0, width, height);
return scaledImage;
}
}
catch (Exception Exc)
{
Exc.ToString();
return scaledImage;
}
}
This method returns the image defined in pixel data.
On button click,
First Save this image in TEST Folder.Then show the original image in first image.
FileUpload1.SaveAs(MapPath("~/TEST/" + FileUpload1.FileName));
Image1.ImageUrl = "~/TEST/" + FileUpload1.FileName;
Then call the above method and pass parameters such as height,width and stream of posted file for resizing image,
Bitmap bitmapImage = ResizeImage(FileUpload1.PostedFile.InputStream,Convert.ToInt32(TextBox2.Text),Convert.ToInt32(TextBox1.Text));
And show that resizing image pixel data in another image.
System.IO.MemoryStream stream = new System.IO.MemoryStream();
bitmapImage.Save(stream, System.Drawing.Imaging.ImageFormat.Bmp);
Image2.ImageUrl = "data:image/png;base64," + Convert.ToBase64String(stream.ToArray(), 0, stream.ToArray().Length);
Write this Following code in button click event :
protected void Button1_Click(object sender, EventArgs e)
{
try
{
FileUpload1.SaveAs(MapPath("~/TEST/" + FileUpload1.FileName));
Image1.ImageUrl = "~/TEST/" + FileUpload1.FileName;
Bitmap bitmapImage = ResizeImage(FileUpload1.PostedFile.InputStream,Convert.ToInt32(TextBox2.Text),Convert.ToInt32(TextBox1.Text));
System.IO.MemoryStream stream = new System.IO.MemoryStream();
bitmapImage.Save(stream, System.Drawing.Imaging.ImageFormat.Bmp);
Image2.ImageUrl = "data:image/png;base64," + Convert.ToBase64String(stream.ToArray(), 0, stream.ToArray().Length);
}
catch (Exception ex)
{
Response.Write("<script>alert('Error Message:')</script>" + ex.Message);
}
}
Output:
Step-1:
Create an ASP.net application and add a webform in it. In design page, add FileUpload control for uploading an image, two textbox for width and height, two image control first one for to show real size of image and the second one for resized image and add a button for submit your details . Follow the source code in your design page...
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
.auto-style1 {
width: 100%;
}
.auto-style2 {
}
.auto-style3 {
width: 332px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<table class="auto-style1">
<tr>
<td class="auto-style2">upload an image :</td>
<td class="auto-style3">
<asp:FileUpload ID="FileUpload1" runat="server" />
</td>
<td class="auto-style3">Width:
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</td>
<td class="auto-style3">Height :<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
</td>
<td>
<asp:Button ID="Button1" runat="server" Text="Resize Image" OnClick="Button1_Click" />
</td>
</tr>
<tr>
<td class="auto-style2" colspan="5">Original :<br />
<asp:Image ID="Image1" runat="server" />
<br />
<br />
After Resizing:<br />
<asp:Image ID="Image2" runat="server" />
<br />
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
Step-2:
Write method for resize an image,
public static Bitmap ResizeImage(Stream stream, int hght, int wdth)
{
int height = hght;
int width = wdth;
Bitmap scaledImage = new Bitmap(width, height);
try
{
System.Drawing.Image originalImage = Bitmap.FromStream(stream);
using (Graphics g = Graphics.FromImage(scaledImage))
{
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.DrawImage(originalImage, 0, 0, width, height);
return scaledImage;
}
}
catch (Exception Exc)
{
Exc.ToString();
return scaledImage;
}
}
This method returns the image defined in pixel data.
On button click,
First Save this image in TEST Folder.Then show the original image in first image.
FileUpload1.SaveAs(MapPath("~/TEST/" + FileUpload1.FileName));
Image1.ImageUrl = "~/TEST/" + FileUpload1.FileName;
Then call the above method and pass parameters such as height,width and stream of posted file for resizing image,
Bitmap bitmapImage = ResizeImage(FileUpload1.PostedFile.InputStream,Convert.ToInt32(TextBox2.Text),Convert.ToInt32(TextBox1.Text));
And show that resizing image pixel data in another image.
System.IO.MemoryStream stream = new System.IO.MemoryStream();
bitmapImage.Save(stream, System.Drawing.Imaging.ImageFormat.Bmp);
Image2.ImageUrl = "data:image/png;base64," + Convert.ToBase64String(stream.ToArray(), 0, stream.ToArray().Length);
Write this Following code in button click event :
protected void Button1_Click(object sender, EventArgs e)
{
try
{
FileUpload1.SaveAs(MapPath("~/TEST/" + FileUpload1.FileName));
Image1.ImageUrl = "~/TEST/" + FileUpload1.FileName;
Bitmap bitmapImage = ResizeImage(FileUpload1.PostedFile.InputStream,Convert.ToInt32(TextBox2.Text),Convert.ToInt32(TextBox1.Text));
System.IO.MemoryStream stream = new System.IO.MemoryStream();
bitmapImage.Save(stream, System.Drawing.Imaging.ImageFormat.Bmp);
Image2.ImageUrl = "data:image/png;base64," + Convert.ToBase64String(stream.ToArray(), 0, stream.ToArray().Length);
}
catch (Exception ex)
{
Response.Write("<script>alert('Error Message:')</script>" + ex.Message);
}
}
Output:
1 Comments
Vexcoobgo Justin Studdard click
ReplyDeletevorspeperdmiss