Dynamically Resize images In Code Behind

2 posts, 0 answers
  1. Dave Miller
    Dave Miller avatar
    64 posts
    Member since:
    Oct 2004

    Posted 24 Nov 2008 Link to this post

    What would be the most optimized way to resize an image for a PictureBox in Itemdatabound or itemDatabinding for a report?  I have quite a few images and am tying not to have to manually resize them.

    I seem to be missing something

            private void detail_ItemDataBound(object sender, EventArgs e)  
                Telerik.Reporting.Processing.DetailSection procDetail = sender as Telerik.Reporting.Processing.DetailSection;  
                Telerik.Reporting.Processing.PictureBox procPictureBox = procDetail.Items["pictureBox1"as Telerik.Reporting.Processing.PictureBox;  
                DataRowView row = procDetail.DataItem as DataRowView;  
                string strPath = HttpContext.Current.Server.MapPath(@"~\Img\Product\" + (string)row["ProdImgLRes"]);  
                using (Bitmap bmpIn = new Bitmap(System.Drawing.Image.FromFile(strPath)))  
                    int w = bmpIn.Width;  
                    int h = bmpIn.Height;  
                    int intWidth = 90;  
                    int intHeight = (int)(h * intWidth) / w;  
                    Bitmap bmpOut = new Bitmap(intWidth, intHeight);  
                    using (Graphics g = Graphics.FromImage(bmpIn))  
                        g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;  
                        g.DrawImage(bmpOut, 0, 0, intWidth, intHeight);  
                    MemoryStream ms = new MemoryStream();  
                    bmpOut.Save(ms, ImageFormat.Bmp);  
                    procPictureBox.Image = Image.FromStream(ms);  




  2. rh
    rh avatar
    193 posts
    Member since:
    Jan 2007

    Posted 24 Nov 2008 Link to this post

    Hi Dave, the .NET framework seems to be a bit finicky in this regard, but I've had luck with code posted at this thread. Let me know if you have any questions, this burned a lot of my cycles.

  3. DevCraft R3 2016 release webinar banner
Back to Top