How to convert BLOB scaling size into same pixel or percentage to create image in C#. RRS feed

  • Question

  • Hi Folks,

    I am a developer and in one of my project I have used crystal report.

    In crystal report I have used BLOB field and I have set Scaling size of this with 15% height and 15% width. Its working as expected with this, it is showing the image appropriately even image is big or small with proper height and width.

    Now, I want to create dynamic html string and use the image with same size in C# using html control with img tag, so I just want to know how I can convert the scale size into pixel or percentage using C# to create image or how I can calculate the height and width ,so it will display image same as blob field in C#

    Overall, How I can convert the scale size of BLOB field into pixel or percentage using C# code or is their any mechanism to calculate height and width percentage same as blob field.

    Please help me on this.

    Wednesday, May 13, 2020 7:03 PM

All replies

    According to my understanding, it is not easy to convert BLOB scaling size into same pixel.
    And I suggest you scale a image by a specified percentage.
    Here is a simple code example you can refer to.

    static Image FixedSize(Image imgPhoto, int Width, int Height)
            int sourceWidth = imgPhoto.Width;
            int sourceHeight = imgPhoto.Height;
            int sourceX = 0;
            int sourceY = 0;
            int destX = 0;
            int destY = 0;
            float nPercent = 0;
            float nPercentW = 0;
            float nPercentH = 0;
            nPercentW = ((float)Width / (float)sourceWidth);
            nPercentH = ((float)Height / (float)sourceHeight);
            if (nPercentH < nPercentW)
                nPercent = nPercentH;
                destX = System.Convert.ToInt16((Width - (sourceWidth * nPercent)) / 2);
                nPercent = nPercentW;
                destY = System.Convert.ToInt16((Height - (sourceHeight * nPercent)) / 2);
            int destWidth = (int)(sourceWidth * nPercent);
            int destHeight = (int)(sourceHeight * nPercent);
            Bitmap bmPhoto = new Bitmap(Width, Height, PixelFormat.Format24bppRgb);
            //Sets the resolution for this Bitmap.
            //Creates a new Graphics 
            Graphics grPhoto = Graphics.FromImage(bmPhoto);
            //Clears the entire drawing surface and fills it with the specified background color.
            grPhoto.InterpolationMode =InterpolationMode.HighQualityBicubic;
            //Draws the specified Image at the specified location and with the original size.
            grPhoto.DrawImage(imgPhoto,new Rectangle(destX, destY, destWidth, destHeight), new Rectangle(sourceX, sourceY, sourceWidth, sourceHeight), GraphicsUnit.Pixel);
            return bmPhoto;

    Here are some related documents you can refer to.
    [c# Image resizing to different size while preserving aspect ratio]
    [Resize image with percentage value in C#]
    Hope these are helpful to you.
    Best Regards,
    Daniel Zhang

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, May 29, 2020 2:13 AM