How to reduce image size in Asp.Net



In this example we will see how to reduce image size in dot net. We will use file upload controls and save image in server folder. And while upload we will reduce image size in code behind. Here is the code snippets.

HTML CODE:
<div>
        <div>
            <asp:Panel ID="Panel1" runat="server">
                <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
                <pre runat="server" id="pre1">
            <asp:FileUpload ID="fileupload1" runat="server" /></pre>
             <asp:Button ID="btnsave" runat="server" Text="Upload" OnClick="btnsave_Click" />
            </asp:Panel>
        </div>
        <div>
            <asp:DataList ID="dtlist" runat="server" RepeatColumns="3" CellPadding="5">
                <ItemTemplate>
                    <asp:Image ID="Image1" ImageUrl='<%# Bind("Name", "~/Images1/{0}") %>' runat="server"
                        Width="200px" Height="200px" />
                    <br />
                    <asp:HyperLink ID="HyperLink1" Text='<%# Bind("Name") %>' NavigateUrl='<%# Bind("Name", "~/Images1/{0}") %>'
                        runat="server" />
                    <asp:LinkButton ID="lb" runat="server"></asp:LinkButton>
                </ItemTemplate>
                <ItemStyle  BorderWidth="3px" HorizontalAlign="Center"
                    VerticalAlign="Bottom" />
            </asp:DataList>
        </div>
        </asp:Panel>
    </div>

CODE BEHIND:
protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindDataList();
            }
        }
protected void BindDataList()
        {
            DirectoryInfo dir = new DirectoryInfo(MapPath("Images1"));
            FileInfo[] files = dir.GetFiles();
            ArrayList listItems = new ArrayList();
            foreach (FileInfo info in files)
            {
                listItems.Add(info);
            }
            dtlist.DataSource = listItems;
            dtlist.DataBind();
        }
protected void btnsave_Click(object sender, EventArgs e)
        {
            try
            {
                if (true)
                {
                    string filename = Path.GetFileName(fileupload1.PostedFile.FileName);
                    string targetPath = Server.MapPath("Images1/" + filename);
                    Stream strm = fileupload1.PostedFile.InputStream;
                    var targetFile = targetPath;
                    //Based on scalefactor image size will vary
                    GenerateThumbnails(0.5, strm, targetFile);
                    BindDataList();
                }
                else
                {
                    Label1.Text = "File size not valid";
                }
            }
            catch (Exception)
            {

                throw;
            }
        }
private voidGenerateThumbnails(double scaleFactor,Stream sourcePath,string targetPath)
        {

            using (var image = Image.FromStream(sourcePath))
            {
                // can give static width (e.g. 1024) of image as we want
                var newWidth = (int)(scaleFactor * image.Width);
                //You can give static height (e.g. 1024) of image as we want
                var newHeight = (int)(scaleFactor * image.Height);
                var thumbnailImg = new Bitmap(newWidth, newHeight);
                var thumbGraph = Graphics.FromImage(thumbnailImg);
                thumbGraph.CompositingQuality = CompositingQuality.HighQuality;
                thumbGraph.SmoothingMode = SmoothingMode.HighQuality;
                thumbGraph.InterpolationMode = InterpolationMode.HighQualityBicubic;
                var imageRectangle = new Rectangle(0, 0, newWidth, newHeight);
                thumbGraph.DrawImage(image, imageRectangle);
                thumbnailImg.Save(targetPath, image.RawFormat);
            }
        }

Here we will compress image size while upload image in server folder and display in data list controls.

Related Posts

Previous
Next Post »

2 comments

comments
Anonymous
October 23, 2019 at 8:43:00 AM GMT+5:30 delete

It's really a cool and useful piece of info. I am satisfied that you just
shared this helpful information with us. Please stay us informed like this.

Thanks for sharing.

Reply
avatar
Anonymous
November 20, 2019 at 7:21:00 AM GMT+5:30 delete

Genuinely when someone doesn't be aware of after that its up to other visitors that they will help, so here it takes place.

Reply
avatar

Thanks for comments.....