This is a migrated thread and some comments may be shown as answers.

Dynamically Resize images In Code Behind

1 Answer 228 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Dave Miller
Top achievements
Rank 2
Dave Miller asked on 25 Nov 2008, 01:38 AM

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);  
            } 

Thanks,
Dave

 

 

1 Answer, 1 is accepted

Sort by
0
rh
Top achievements
Rank 1
answered on 25 Nov 2008, 03:56 AM
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.

http://www.telerik.com/community/forums/reporting/telerik-reporting/image-value-parameter-is-not-valid.aspx
Tags
General Discussions
Asked by
Dave Miller
Top achievements
Rank 2
Answers by
rh
Top achievements
Rank 1
Share this question
or