Results 1 to 3 of 3

Thread: Converting an Image file to a Bitmap and maintain aspect ratio?

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    5,034

    Converting an Image file to a Bitmap and maintain aspect ratio?

    I'm creating a web image viewing app in C#. I have a section of code that will allow for upload of an Image file (*.gif, *.jpg, etc.). I take this image and make an ImageButton with a pixel size of 100x100. When I click on one of the ImageButtons, I need to be able to take the original Image File and reduce the size by 40% and still maintain it's aspect ratio...but I don't quite know how to do this. The below code is what I am working with but there are holes in the code.

    Code:
            void UploadImpage()      //this routine works as it should
            {
                foreach (string filename in Directory.GetFiles(Server.MapPath("~/Data/")))
                {
                    ImageButton imgButton = new ImageButton();
                    FileInfo flInfo = new FileInfo(filename);
    
                    imgButton.ImageUrl = "~/Data/" + flInfo.Name;
                    imgButton.Width = Unit.Pixel(100);
                    imgButton.Height = Unit.Pixel(100);
                    imgButton.Style.Add("padding", "5px");
                    imgButton.Click += new ImageClickEventHandler(imgButton_Click);
                    Panel1.Controls.Add(imgButton);
                }
            }
            //
            //
            //
            void imgButton_Click(object sender,ImageClickEventArgs e)
            {
                ImageButton imgBtn = (ImageButton)sender;         //casting the selected ImageButton
    
                System.Drawing.Image img;
    
                //Here is where I'm lost
                img.Height = imgBtn.Height;
    
                Bitmap Img = new Bitmap(x, y);
                using (Graphics gr = Graphics.FromImage(Img))
                {
                    gr.SmoothingMode = SmoothingMode.HighQuality;
                    gr.InterpolationMode = InterpolationMode.HighQualityBicubic;
                    gr.PixelOffsetMode = PixelOffsetMode.HighQuality;
                    gr.DrawImage(p, new Rectangle(0, 0, x, y));
                }
    
                Response.Redirect("~/WebForm2.aspx?ImageURL=" + ((ImageButton)sender).ImageUrl);
            }
    Blake

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,930

    Re: Converting an Image file to a Bitmap and maintain aspect ratio?

    Is this what you are after?
    Code:
                img.Height = imgBtn.Height * 0.6;
                img.Width = img.Height;

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    5,034

    Re: Converting an Image file to a Bitmap and maintain aspect ratio?

    Si,

    This is what I'm looking for and actually thought of this. However, the image button dimensions are 100px x 100px. If I use the ImageButton properties of Height and Width...do they hold the size value of the original image or what it was set to...in my case 100px?

    Thanks,
    Blake

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width