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 5976491
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T21:14:11+00:00 2026-05-22T21:14:11+00:00

I have a folder containing about 2500 PNG images, with no transparency. Every image

  • 0

I have a folder containing about 2500 PNG images, with no transparency. Every image is about 500 x 500 (some are 491 x 433, others 511 x 499 etc).

I want to programatically downsize every image to 10% of its original size, and to set the white background of every image as the transparent color.

To test the functionality of my application without resizing 2500 images every time, I used 15 images of billiard balls as a “test” folder.

Now my problem is with the following code, I get a resized and cropped PNG, whith a almost transparent background. The problem is that a white border on the left and top appears in every image viewer (Irfan View, Paint.Net and GIMP)

How can I avoid this border?

Here is the code I used for this:

void ResizeI(string[] Paths, string OutPut, Methods m, PointF Values, bool TwoCheck, bool Overwrite, float[] CropVals)
    {
        for (int i = 0; i < Paths.Length; i++)//Paths is the array of all images
        {
            string Path = Paths[i];//current image
            Bitmap Oimg = (Bitmap)Bitmap.FromFile(Path);//original image
            Bitmap img = new Bitmap((int)(Oimg.Width - CropVals[0] - CropVals[1]), (int)(Oimg.Height - CropVals[2] - CropVals[3]));//cropped image
            Graphics ggg = Graphics.FromImage(img);
            ggg.DrawImage(Oimg, new RectangleF(((float)-CropVals[0]), ((float)-CropVals[2]), Oimg.Width - CropVals[1], Oimg.Height - CropVals[3]));
            ggg.Flush(System.Drawing.Drawing2D.FlushIntention.Flush);
            ggg.Dispose();
            PointF scalefactor = GetScaleFactor(img, Values, TwoCheck);//the scale factor equals 0.1 for 10%
            Bitmap newimg = new Bitmap((int)(Math.Ceiling(((float)img.Width) * scalefactor.X)), (int)(Math.Ceiling(((float)img.Height) * scalefactor.Y)));
            System.Drawing.Imaging.ImageFormat curform = img.RawFormat;
            string OutPath = System.IO.Path.Combine(OutPut, System.IO.Path.GetFileName(Path));
            OutPath = CheckPath(OutPath, Overwrite);//Delete if exsits
            Graphics g = Graphics.FromImage(newimg);
            g.InterpolationMode = GetModeFromMethod(m);//Bicubic interpolation
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            g.ScaleTransform(scalefactor.X, scalefactor.Y);
            g.DrawImage(img, new Rectangle(0, 0, (int)Math.Ceiling(((float)newimg.Width) / scalefactor.X) + 1, (int)Math.Ceiling(((float)newimg.Height) / scalefactor.Y) + 1));
            //g.Flush(System.Drawing.Drawing2D.FlushIntention.Flush);
            newimg.MakeTransparent(Color.White);
            newimg.Save(OutPath, curform);
            g.Dispose();
            img.Dispose();
        }
    }

And here is a example of the white border I mentioned. Download the image or drag it around and put a black background under it to see the border:

The resized Image and the white border

— EDIT —

I managed to write this function instead of newimg.MakeTransparent(...):

 void SetTransparent(ref Bitmap b)
    {
        for (int i = 0; i < b.Width; i++)
        {
            for (int ii = 0; ii < b.Height; ii++)
            {
                Color cc = b.GetPixel(i, ii);
                int tog = cc.R + cc.G + cc.B;
                float durch = 255f - (((float)tog) / 3f);
                b.SetPixel(i, ii, Color.FromArgb((int)durch, cc.R, cc.G, cc.B));
            }
        }
    }

the problem is that my billiard ball now look like this:

Second version of the billiard ball

  • 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-22T21:14:12+00:00Added an answer on May 22, 2026 at 9:14 pm

    I can’t help with the specific code, but maybe can explain what’s happening.

    newimg.MakeTransparent(Color.White);
    

    This will take one color, and make it transparent. The catch is that, there’s a spectrum of colors between the edge of your billiard ball (orange) and the pure white background. This is the antialiasing of the edge (which will be a blend of colors from the pure orange of the ball to the pure white of the background).

    By turning only pure white transparent, you are still left with this ‘halo’ of white-ish colors around the object.

    There’s perhaps a better way to handle this using white values as an alpha mask but I’m not sure if .net’s image library can handle that (I’ll have to defer to someone with more .net experience comes along).

    In the interim, though, what may help is if you set the transparency before you do the resize. It won’t be a true fix, but might reduce the halo effect some.

    UPDATE:

    So, I’ve been thinking about this some more, and I’m not entirely sure there’s a programmatic solution for creating alpha channel transparency automatically, as I have a hunch there’s a lot of subjectivity involved.

    Off the top of my head, this is what I came up with:

    • assuming the top left pixel is your 100% transparent color (we’ll say pixel X).
    • assuming your background that you want transparent is one solid color (vs. a pattern)
    • assume a roughly 3px anti-aliasing

    you could then…

    • check for neighboring pixels to X. For each neighboring pixel to X that matches the color of X, we set that 100% transparent.
    • if a pixel next to x is NOT the same, we could check it’s relative hue.
    • branch from that pixel and check it’s surrounding pixels.
    • do this marking each pixel (a, b, c, etc) until the relative hue changes a certain percentage and/or the pixel color is the same as it’s neighbor (with a certain margin of variability). If it does, we’ll assume we’re well into the interior of the object.
    • now step backwards through the pixels you marked, adjusting the transparency…say c=0% b=33% a=66%

    But still, that’s a large oversimplification of what would really have to happen. It’s making a lot of assumptions, not taking into account a patterned background, and completely ignores interior areas that need to also be transparent (such as a donut hole).

    Normally in a graphics editing app, this is done via selecting blocks of the background color, feathering the edges of said selection, then turning that into an alpha max.

    It’s a really interesting question/problem. I, alas, don’t have the answer for you but will be watching this thread with curiosity!

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

Sidebar

Related Questions

I have a folder containing over 200 raw images, i want to convert all
I have an asp.net application containing pages that lie in multiple folder. I have
I have data table containing one column as FilePath. FilePath D:\New folder\link.txt D:\New folder\SharepointMigration(Work
I have a folder called Etc which has an image I want to use
I have a folder on my remote server that has a few .png files
I have a Windows box and a folder containing such files: 2010-07-04 20:18 81
I have a folder containing a set of queries in Cognos. I have tried
On my hard drive I have a folder containing a number of Excel workbooks.
in my project i have a folder containing N number of files. I need
Is there any way, with the Eclipse IDE, to have a master folder containing

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.