locked
Thumbnail from db and keeping aspect ratio RRS feed

  • Question

  • User2006713638 posted

    What is a good way for loading image as thumbnail from database and maintain its exact aspect ratio? the box of output image should remain same, but image inside should keep original proportions.

    currently I generate thumbnail with GetThumbnailImage method. and set heigth and width (for all looking same in search result). this is fine for most images, but sometimes outcoming image aspect ratio is bad (depending on original image user entered). Below is my code the way I do now. Is there any ways to improve it?

     

    public void ProcessRequest(HttpContext context)
            {
                Int32 car_image_id;
                if (context.Request.QueryString["car_image_id"] != null)
                    car_image_id = Convert.ToInt32(context.Request.QueryString["car_image_id"]);
                else
                    throw new ArgumentException("No parameter specified");
    
                
                using (FbConnection conn = new FbConnection(ConfigurationManager.ConnectionStrings["ConnectionString2"].ConnectionString))
                {
                    conn.Open();
                    using (FbCommand cmd = conn.CreateCommand())
                    {
                        cmd.CommandText = "SELECT imagefile FROM car_image WHERE car_image_id = ?;";
                        cmd.CommandType = CommandType.Text;
                        cmd.Parameters.Add("@car_image_id", car_image_id);
                        object img = cmd.ExecuteScalar();
    
                        byte[] bytes = null;
                        bytes = (byte[])img;
                        System.IO.MemoryStream streamBitmap = new System.IO.MemoryStream(bytes);
                        Bitmap objBm = new Bitmap((Bitmap)System.Drawing.Image.FromStream(streamBitmap));
    
                        //Generate the Thumbnails
                        Bitmap objb2 = new Bitmap(objBm.GetThumbnailImage(80, 60, null, IntPtr.Zero));
    
                        //Converting Bitmap to Byte array
                        System.IO.MemoryStream stream = new System.IO.MemoryStream();
                        objb2.Save(stream, ImageFormat.Jpeg);
                                           
    
                        byte[] bmpBytes = stream.GetBuffer();
    
                        context.Response.ContentType = "image/jpeg";
                        context.Response.BinaryWrite(bmpBytes);
    
                        objb2.Dispose();
                        objBm.Dispose();
    
                        bytes = null;
                        
                        context.Response.End();  
                        
    
                    }
                    conn.Close();
                }
            }

    Thursday, February 2, 2012 3:56 PM

Answers

  • User1508394307 posted

    Try this

    Bitmap objBm = new Bitmap((Bitmap)System.Drawing.Image.FromStream(streamBitmap));   
    
    // add a new piece of code from here
    orgwidth = objBm.Width;
    orgheight = objBm.Height;
     
    imgwidth = objBm.Width;
    imgheight = objBm.Height;
     
    // if 80 is max width
    imgwidth = 80;
    imgheight = Convert.ToInt32((imgwidth * orgheight) / orgwidth);
     
    // otherwise, set max height 
    //imgheight = 60;
    //imgwidth = Convert.ToInt32((imgheight * orgwidth) / orgheight);
    
    //Generate the Thumbnails
     
    // get rid of your line
    //Bitmap objb2 = new Bitmap(objBm.GetThumbnailImage(80, 60, null, IntPtr.Zero)); 
    
    //and replace it by the new one 
    Bitmap objb2 = new Bitmap(objBm.GetThumbnailImage(imgwidth, imgheight, null, IntPtr.Zero));

    Hope this helps.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, February 5, 2012 10:00 AM