Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7088485
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:48:24+00:00 2026-05-28T07:48:24+00:00

I have a function: private static Image ScaleAndPadBlack(Image original_img, Thumbnail thumb) { if (thumb.OptimalSizeHeight

  • 0

I have a function:

    private static Image ScaleAndPadBlack(Image original_img, Thumbnail thumb)
    {
        if (thumb.OptimalSizeHeight <= 0)
        {
            throw new ArgumentOutOfRangeException("OptimalSizeHeight", "OptimalSizeHeight must be declared and positive when using the ScaleAndCrop method.");
        }
        if (thumb.OptimalSizeWidth <= 0)
        {
            throw new ArgumentOutOfRangeException("OptimalSizeWidth", "OptimalSizeWidth must be declared and positive when using the ScaleAndCrop method.");
        }

        Size dest_size = new Size(thumb.OptimalSizeWidth, thumb.OptimalSizeHeight);

        decimal act_width_rate = (decimal)dest_size.Width / (decimal)original_img.Width;
        decimal act_height_rate = (decimal)dest_size.Height / (decimal)original_img.Height;

        decimal scale_rate;
        if (act_width_rate <= act_height_rate)
            scale_rate = act_width_rate;
        else
            scale_rate = act_height_rate;

        Size act_size = new Size(Convert.ToInt32(original_img.Width * scale_rate), Convert.ToInt32(original_img.Height * scale_rate));

        var res = resizeImage(original_img, act_size);

        var b = new Bitmap(thumb.OptimalSizeWidth, thumb.OptimalSizeHeight);
        var g = Graphics.FromImage(b);

        Point p = new Point(((dest_size.Width - act_size.Width) / 2), ((dest_size.Height - act_size.Height) / 2));
        g.FillRectangle(Brushes.Black, new Rectangle(new Point(0, 0), dest_size));
        g.SmoothingMode = SmoothingMode.None;
        g.InterpolationMode = InterpolationMode.NearestNeighbor;
        g.DrawImage(res, p);
        res.Dispose();
        res = null;
        return b;
    }

what this does is it resizes the image to fit inside a bounding box, creating a new image, fill it with black and put the resized image onto the black image.

The problem is that resizing a fully white jpg (761×896) image into a 270×180 bounding box results in this:

Greyish bar on top

You can see that there is a grey bar at the top of the image which is because the resized image somehow has a transparent edge.

Why is this happening? Resizing an image can result in transparent edges? That is definately not desired in this case.

Or is that top edge there because some other reason?

How should I write this function that there are no black-grey edges on cases like this? I want only to fill the part where there is no image, should I do something completely differently?

EDIT:

The g.SmoothingMode = SmoothingMode.None; g.InterpolationMode = InterpolationMode.NearestNeighbor; are there because I was testing this issue. They does not seem to matter.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T07:48:25+00:00Added an answer on May 28, 2026 at 7:48 am

    I have changed the drawimage and fillrectangle part to:

            g.DrawImage(res, p);            
            //left
            g.FillRectangle(Brushes.Black, new Rectangle(0, 0, p.X, dest_size.Height));
            //right
            g.FillRectangle(Brushes.Black, new Rectangle(p.X + act_size.Width, 0, p.X, dest_size.Height));
            //up
            g.FillRectangle(Brushes.Black, new Rectangle(0, 0, dest_size.Width, p.Y));
            //down
            g.FillRectangle(Brushes.Black, new Rectangle(0, p.Y + act_size.Width, dest_size.Width, p.Y));
    

    and it seems to be working with my test cases. Still I’m not sure what caused the original issue.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following function: static private Image CropRotate(Image wholeImage, Rectangle cropArea) { Bitmap
I have this function... private string dateConvert(string datDate) { System.Globalization.CultureInfo cultEnGb = new System.Globalization.CultureInfo(en-GB);
I have a function that draw an image on graphics: private void DrawSmallImage(Graphics g)
Suppose you have private static const INCLUDE_MY_DEBUG_CODE:Boolean = false; public function runMyDebugCode():void { if
I have some function like private static UserInfo FillUserInfoFromDataRow(DataRow dr) { UserInfo user =
I have the following function: private static void prettyPrint(Document doc, File destFile) { TransformerFactory
I have a function like this in actionscript3 private function uploadFile(event:MouseEvent):void { var uploader:URLRequest
I have a label function like : private function formatDate (item:Object, column:DataGridColumn):String { var
I have the following simple function: private void EnableDisable941ScheduleBButton() { if (this._uosDepositorFrequency.Value != null)
I have the following serialization method: Private Function SerializeData(ByVal data As cData) As String

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.