Esempio n. 1
0
        private static unsafe Bitmap DifferentiateAlpha(Bitmap whiteBitmap, Bitmap blackBitmap)
        {
            if (whiteBitmap == null || blackBitmap == null || whiteBitmap.Width != blackBitmap.Width || whiteBitmap.Height != blackBitmap.Height)
            {
                return(null);
            }
            int sizeX = whiteBitmap.Width;
            int sizeY = whiteBitmap.Height;
            var final = new Bitmap(sizeX, sizeY, PixelFormat.Format32bppArgb);
            var a     = new UnsafeBitmap(whiteBitmap);
            var b     = new UnsafeBitmap(blackBitmap);
            var f     = new UnsafeBitmap(final);

            a.LockImage();
            b.LockImage();
            f.LockImage();

            bool empty = true;

            for (int x = 0, y = 0; x < sizeX && y < sizeY;)
            {
                PixelData *pixelA = a.GetPixel(x, y);
                PixelData *pixelB = b.GetPixel(x, y);
                PixelData *pixelF = f.GetPixel(x, y);

                pixelF->Alpha = ToByte((pixelB->Red - pixelA->Red + 255 + pixelB->Green - pixelA->Green + 255 + pixelB->Blue - pixelA->Blue + 255) / 3);
                if (pixelF->Alpha > 0)
                {
                    // Following math creates an image optimized to be displayed on a black background
                    pixelF->Red   = ToByte(255 * pixelB->Red / pixelF->Alpha);
                    pixelF->Green = ToByte(255 * pixelB->Green / pixelF->Alpha);
                    pixelF->Blue  = ToByte(255 * pixelB->Blue / pixelF->Alpha);

                    // Following math creates an image optimized to be displayed on a white background

                    /*
                     * pixelF->Red = ToByte(255 * (pixelA->Red + pixelF->Alpha - 255) / pixelF->Alpha);
                     * pixelF->Green = ToByte(255 * (pixelA->Green + pixelF->Alpha - 255) / pixelF->Alpha);
                     * pixelF->Blue = ToByte(255 * (pixelA->Blue + pixelF->Alpha - 255) / pixelF->Alpha);
                     */
                }
                if (empty && pixelF->Alpha > 0)
                {
                    empty = false;
                }

                if (x == sizeX - 1)
                {
                    y++;
                    x = 0;
                    continue;
                }
                x++;
            }

            a.UnlockImage();
            b.UnlockImage();
            f.UnlockImage();
            return(empty ? null : final);
        }
Esempio n. 2
0
        private static unsafe Bitmap CreateMask(Bitmap bitmap, int minAlpha)
        {
            if (bitmap == null)
            {
                return(null);
            }
            int sizeX = bitmap.Width;
            int sizeY = bitmap.Height;
            var final = new Bitmap(sizeX, sizeY, PixelFormat.Format32bppArgb);
            var a     = new UnsafeBitmap(bitmap);
            var f     = new UnsafeBitmap(final);

            a.LockImage();
            f.LockImage();

            for (int x = 0, y = 0; x < sizeX && y < sizeY;)
            {
                PixelData *pixelA = a.GetPixel(x, y);
                PixelData *pixelF = f.GetPixel(x, y);

                if (pixelA->Alpha > minAlpha)
                {
                    pixelF->Blue  = 0;
                    pixelF->Green = 0;
                    pixelF->Red   = 0;
                    pixelF->Alpha = 255;
                }

                if (x == sizeX - 1)
                {
                    y++;
                    x = 0;
                    continue;
                }
                x++;
            }

            a.UnlockImage();
            f.UnlockImage();
            return(final);
        }
Esempio n. 3
0
        private static unsafe Bitmap GenerateChecker(int s)
        {
            var b1 = new Bitmap(s * 2, s * 2, PixelFormat.Format24bppRgb);
            var b  = new UnsafeBitmap(b1);

            b.LockImage();
            PixelData *pixel;

            for (int x = 0, y = 0; x < s * 2 && y < s * 2;)
            {
                pixel = b.GetPixel(x, y);
                if ((x >= 0 && x <= s - 1) && (y >= 0 && y <= s - 1))
                {
                    pixel->SetAll(255);
                }
                if ((x >= s && x <= s * 2 - 1) && (y >= 0 && y <= s - 1))
                {
                    pixel->SetAll(200);
                }
                if ((x >= 0 && x <= s - 1) && (y >= s && y <= s * 2 - 1))
                {
                    pixel->SetAll(200);
                }
                if ((x >= s && x <= s * 2 - 1) && (y >= s && y <= s * 2 - 1))
                {
                    pixel->SetAll(255);
                }
                if (x == s * 2 - 1)
                {
                    y++;
                    x = 0;
                    continue;
                }
                x++;
            }
            b.UnlockImage();
            return(b1);
        }
Esempio n. 4
0
        private static unsafe Bitmap DifferentiateAlpha(Bitmap whiteBitmap,
                                                        Bitmap blackBitmap) {
            if (whiteBitmap == null || blackBitmap == null ||
                whiteBitmap.Width != blackBitmap.Width ||
                whiteBitmap.Height != blackBitmap.Height)
                return null;
            int sizeX = whiteBitmap.Width;
            int sizeY = whiteBitmap.Height;
            var final = new Bitmap(sizeX, sizeY, PixelFormat.Format32bppArgb);
            var a = new UnsafeBitmap(whiteBitmap);
            var b = new UnsafeBitmap(blackBitmap);
            var f = new UnsafeBitmap(final);
            a.LockImage();
            b.LockImage();
            f.LockImage();

            bool empty = true;

            for (int x = 0, y = 0; x < sizeX && y < sizeY;) {
                PixelData* pixelA = a.GetPixel(x, y);
                PixelData* pixelB = b.GetPixel(x, y);
                PixelData* pixelF = f.GetPixel(x, y);

                pixelF->Alpha =
                    ToByte((pixelB->Red - pixelA->Red + 255 + pixelB->Green -
                            pixelA->Green + 255 + pixelB->Blue - pixelA->Blue +
                            255)/3);
                if (pixelF->Alpha > 0) {
                    // Following math creates an image optimized to be displayed on a black background
                    pixelF->Red = ToByte(255*pixelB->Red/pixelF->Alpha);
                    pixelF->Green = ToByte(255*pixelB->Green/pixelF->Alpha);
                    pixelF->Blue = ToByte(255*pixelB->Blue/pixelF->Alpha);

                    // Following math creates an image optimized to be displayed on a white background
                    /*pixelF->Red =
                        ToByte(255*(pixelA->Red + pixelF->Alpha - 255)/
                               pixelF->Alpha);
                    pixelF->Green =
                        ToByte(255*(pixelA->Green + pixelF->Alpha - 255)/
                               pixelF->Alpha);
                    pixelF->Blue =
                        ToByte(255*(pixelA->Blue + pixelF->Alpha - 255)/
                               pixelF->Alpha);*/
                }
                if (empty && pixelF->Alpha > 0)
                    empty = false;

                if (x == sizeX - 1) {
                    y++;
                    x = 0;
                    continue;
                }
                x++;
            }

            a.UnlockImage();
            b.UnlockImage();
            f.UnlockImage();
            return empty ? null : final;
        }
Esempio n. 5
0
        private static unsafe Bitmap CropEmptyEdges(Bitmap b1, Color trimColour) {
            if (b1 == null)
                return null;

            int sizeX = b1.Width;
            int sizeY = b1.Height;
            var b = new UnsafeBitmap(b1);
            b.LockImage();

            int left = -1;
            int top = -1;
            int right = -1;
            int bottom = -1;

            PixelData* pixel;

            for (int x = 0, y = 0;;) {
                pixel = b.GetPixel(x, y);
                if (left == -1) {
                    if ((trimColour.A == 0 && pixel->Alpha != 0) ||
                        (trimColour.R != pixel->Red &
                         trimColour.G != pixel->Green &
                         trimColour.B != pixel->Blue)) {
                        left = x;
                        x = 0;
                        y = 0;
                        continue;
                    }
                    if (y == sizeY - 1) {
                        x++;
                        y = 0;
                    } else
                        y++;

                    continue;
                }
                if (top == -1) {
                    if ((trimColour.A == 0 && pixel->Alpha != 0) ||
                        (trimColour.R != pixel->Red &
                         trimColour.G != pixel->Green &
                         trimColour.B != pixel->Blue)) {
                        top = y;
                        x = sizeX - 1;
                        y = 0;
                        continue;
                    }
                    if (x == sizeX - 1) {
                        y++;
                        x = 0;
                    } else
                        x++;

                    continue;
                }
                if (right == -1) {
                    if ((trimColour.A == 0 && pixel->Alpha != 0) ||
                        (trimColour.R != pixel->Red &
                         trimColour.G != pixel->Green &
                         trimColour.B != pixel->Blue)) {
                        right = x + 1;
                        x = 0;
                        y = sizeY - 1;
                        continue;
                    }
                    if (y == sizeY - 1) {
                        x--;
                        y = 0;
                    } else
                        y++;

                    continue;
                }
                if (bottom == -1) {
                    if ((trimColour.A == 0 && pixel->Alpha != 0) ||
                        (trimColour.R != pixel->Red &
                         trimColour.G != pixel->Green &
                         trimColour.B != pixel->Blue)) {
                        bottom = y + 1;
                        break;
                    }
                    if (x == sizeX - 1) {
                        y--;
                        x = 0;
                    } else
                        x++;

                    continue;
                }
            }
            b.UnlockImage();
            if (left >= right || top >= bottom)
                return null;

            Bitmap final =
                b1.Clone(new Rectangle(left, top, right - left, bottom - top),
                         b1.PixelFormat);
            b1.Dispose();
            return final;
        }
Esempio n. 6
0
 private static unsafe Bitmap GenerateChecker(int s) {
     var b1 = new Bitmap(s*2, s*2, PixelFormat.Format24bppRgb);
     var b = new UnsafeBitmap(b1);
     b.LockImage();
     PixelData* pixel;
     for (int x = 0, y = 0; x < s*2 && y < s*2;) {
         pixel = b.GetPixel(x, y);
         if ((x >= 0 && x <= s - 1) && (y >= 0 && y <= s - 1))
             pixel->SetAll(255);
         if ((x >= s && x <= s*2 - 1) && (y >= 0 && y <= s - 1))
             pixel->SetAll(200);
         if ((x >= 0 && x <= s - 1) && (y >= s && y <= s*2 - 1))
             pixel->SetAll(200);
         if ((x >= s && x <= s*2 - 1) && (y >= s && y <= s*2 - 1))
             pixel->SetAll(255);
         if (x == s*2 - 1) {
             y++;
             x = 0;
             continue;
         }
         x++;
     }
     b.UnlockImage();
     return b1;
 }
Esempio n. 7
0
        private static unsafe Bitmap CropEmptyEdges(Bitmap b1, Color trimColor)
        {
            if (b1 == null)
            {
                return(null);
            }

            int sizeX = b1.Width;
            int sizeY = b1.Height;
            var b     = new UnsafeBitmap(b1);

            b.LockImage();

            int left   = -1;
            int top    = -1;
            int right  = -1;
            int bottom = -1;

            PixelData *pixel;

            for (int x = 0, y = 0; ;)
            {
                pixel = b.GetPixel(x, y);
                if (left == -1)
                {
                    if ((trimColor.A == 0 && pixel->Alpha != 0) || (trimColor.R != pixel->Red & trimColor.G != pixel->Green & trimColor.B != pixel->Blue))
                    {
                        left = x;
                        x    = 0;
                        y    = 0;
                        continue;
                    }
                    if (y == sizeY - 1)
                    {
                        x++;
                        y = 0;
                    }
                    else
                    {
                        y++;
                    }

                    continue;
                }
                if (top == -1)
                {
                    if ((trimColor.A == 0 && pixel->Alpha != 0) || (trimColor.R != pixel->Red & trimColor.G != pixel->Green & trimColor.B != pixel->Blue))
                    {
                        top = y;
                        x   = sizeX - 1;
                        y   = 0;
                        continue;
                    }
                    if (x == sizeX - 1)
                    {
                        y++;
                        x = 0;
                    }
                    else
                    {
                        x++;
                    }

                    continue;
                }
                if (right == -1)
                {
                    if ((trimColor.A == 0 && pixel->Alpha != 0) || (trimColor.R != pixel->Red & trimColor.G != pixel->Green & trimColor.B != pixel->Blue))
                    {
                        right = x + 1;
                        x     = 0;
                        y     = sizeY - 1;
                        continue;
                    }
                    if (y == sizeY - 1)
                    {
                        x--;
                        y = 0;
                    }
                    else
                    {
                        y++;
                    }

                    continue;
                }
                if (bottom == -1)
                {
                    if ((trimColor.A == 0 && pixel->Alpha != 0) || (trimColor.R != pixel->Red & trimColor.G != pixel->Green & trimColor.B != pixel->Blue))
                    {
                        bottom = y + 1;
                        break;
                    }
                    if (x == sizeX - 1)
                    {
                        y--;
                        x = 0;
                    }
                    else
                    {
                        x++;
                    }

                    continue;
                }
            }
            b.UnlockImage();
            if (left >= right || top >= bottom)
            {
                return(null);
            }

            Bitmap final = b1.Clone(new Rectangle(left, top, right - left, bottom - top), b1.PixelFormat);

            b1.Dispose();
            return(final);
        }
Esempio n. 8
0
        private static unsafe Bitmap[] CropEmptyEdges(Bitmap[] b1, Color trimColor, ref ScreenshotTask data)
        {
            if (b1 == null)
            {
                return(null);
            }

            int sizeX = b1[0].Width;
            int sizeY = b1[0].Height;
            var b     = new UnsafeBitmap(b1[0]);

            b.LockImage();

            int left   = -1;
            int top    = -1;
            int right  = -1;
            int bottom = -1;

            PixelData *pixel;

            for (int x = 0, y = 0; ;)
            {
                pixel = b.GetPixel(x, y);
                if (left == -1)
                {
                    if ((trimColor.A == 0 && pixel->Alpha != 0) || (trimColor.R != pixel->Red & trimColor.G != pixel->Green & trimColor.B != pixel->Blue))
                    {
                        left = x;
                        x    = 0;
                        y    = 0;
                        continue;
                    }
                    if (y == sizeY - 1)
                    {
                        x++;
                        y = 0;
                    }
                    else
                    {
                        y++;
                    }

                    continue;
                }
                if (top == -1)
                {
                    if ((trimColor.A == 0 && pixel->Alpha != 0) || (trimColor.R != pixel->Red & trimColor.G != pixel->Green & trimColor.B != pixel->Blue))
                    {
                        top = y;
                        x   = sizeX - 1;
                        y   = 0;
                        continue;
                    }
                    if (x == sizeX - 1)
                    {
                        y++;
                        x = 0;
                    }
                    else
                    {
                        x++;
                    }

                    continue;
                }
                if (right == -1)
                {
                    if ((trimColor.A == 0 && pixel->Alpha != 0) || (trimColor.R != pixel->Red & trimColor.G != pixel->Green & trimColor.B != pixel->Blue))
                    {
                        right = x + 1;
                        x     = 0;
                        y     = sizeY - 1;
                        continue;
                    }
                    if (y == sizeY - 1)
                    {
                        x--;
                        y = 0;
                    }
                    else
                    {
                        y++;
                    }

                    continue;
                }
                if (bottom == -1)
                {
                    if ((trimColor.A == 0 && pixel->Alpha != 0) || (trimColor.R != pixel->Red & trimColor.G != pixel->Green & trimColor.B != pixel->Blue))
                    {
                        bottom = y + 1;
                        break;
                    }
                    if (x == sizeX - 1)
                    {
                        y--;
                        x = 0;
                    }
                    else
                    {
                        x++;
                    }

                    continue;
                }
            }
            b.UnlockImage();
            if (left >= right || top >= bottom)
            {
                return(null);
            }

            int rightSize  = right - left;
            int bottomSize = bottom - top;

            if (rightSize >= b1[0].Width || bottomSize >= b1[0].Height)
            {
                MessageBox.Show("Background removal very likely failed, please check the final screenshot after capture.");
            }

            if (data.CropMode) //if crop mode: keep window centered
            {
                if (b1[0].Width - rightSize - left > left)
                {
                    rightSize = b1[0].Width - (2 * left);
                }
                else
                {
                    int oldLeft = left;
                    left       = b1[0].Width - rightSize - left;
                    rightSize += (oldLeft - left);
                }

                if (b1[0].Height - bottomSize - top > top)
                {
                    bottomSize = b1[0].Height - (2 * top);
                }
                else
                {
                    int oldTop = top;
                    top         = b1[0].Height - bottomSize - top;
                    bottomSize += (oldTop - top);
                }
            }

            int canvasRightSize  = rightSize % 2 == 0 ? rightSize : rightSize + 1;
            int canvasBottomSize = bottomSize % 2 == 0 ? bottomSize : bottomSize + 1;

            if (data.DoCanvas)
            {
                canvasRightSize  = data.CanvasX;
                canvasBottomSize = data.CanvasY;
            }

            Bitmap[] final = new Bitmap[b1.Length];
            for (int i = 0; i < b1.Length; i++)
            {
                if (b1[i] == null)
                {
                    continue;
                }


                final[i] = b1[i].Clone(new Rectangle(left, top, rightSize, bottomSize), b1[i].PixelFormat);

                Bitmap temp = new Bitmap(canvasRightSize, canvasBottomSize);
                using (Graphics grD = Graphics.FromImage(temp))
                {
                    grD.DrawImage(final[i], new Rectangle(0, 0, final[i].Width, final[i].Height), new Rectangle(0, 0, final[i].Width, final[i].Height), GraphicsUnit.Pixel);
                }
                final[i].Dispose();
                final[i] = temp;

                b1[i].Dispose();
            }

            return(final);
        }