Пример #1
0
        private void MonohromeFilter(Bitmap source, Bitmap destination, Color firstColor, Color secondColor, int coefficient)
        {
            var initData   = source.LockBits(new Rectangle(0, 0, source.Width, source.Height), ImageLockMode.ReadOnly, source.PixelFormat);
            var destData   = destination.LockBits(new Rectangle(0, 0, destination.Width, destination.Height), ImageLockMode.WriteOnly, source.PixelFormat);
            int byteLength = 4;

            if (source.PixelFormat.ToString().Contains("24"))
            {
                byteLength = 3;
            }

            unsafe
            {
                byte *sourcePtr = (byte *)initData.Scan0.ToPointer();
                byte *destPtr   = (byte *)destData.Scan0.ToPointer();

                for (int i = 0; i < initData.Stride; i += byteLength)
                {
                    for (int j = 0; j < source.Height; j++)
                    {
                        var pixel = PixelOperations.GetPixelUnsafe(sourcePtr, initData.Stride, i, j, byteLength);
                        pixel = GetMonohromePixel(pixel, byteLength, coefficient);
                        PixelOperations.SetPixelUnsafe(destPtr, pixel, initData.Stride, i, j, byteLength);
                    }
                }
            }

            source.UnlockBits(initData);
            destination.UnlockBits(destData);
        }
Пример #2
0
        private Bitmap RotateImage(Bitmap source, double angle)
        {
            double radAngle = angle * Math.PI / 180;
            var    cos      = Math.Cos(radAngle);
            var    sin      = Math.Sin(radAngle);

            var widthAndHeight = CalculateNewBoundaries(bitmap.Width, bitmap.Height, radAngle);
            int newWidth       = widthAndHeight.width;
            int newHeight      = widthAndHeight.height;

            Bitmap destination = new Bitmap((int)newWidth, (int)newHeight);

            var SrcData  = source.LockBits(new Rectangle(0, 0, source.Width, source.Height), ImageLockMode.ReadOnly, source.PixelFormat);
            var DestData = destination.LockBits(new Rectangle(0, 0, destination.Width, destination.Height), ImageLockMode.WriteOnly, source.PixelFormat);

            int bytelength = 4;

            if (source.PixelFormat.ToString().Contains("24"))
            {
                bytelength = 3;
            }

            int x0 = (int)(newWidth / 2.0);
            int y0 = (int)(newHeight / 2.0);

            unsafe
            {
                byte *SrcPtr  = (byte *)SrcData.Scan0.ToPointer();
                byte *DestPtr = (byte *)DestData.Scan0.ToPointer();

                var offset = GetOffsets(source.Width, source.Height, cos, sin, x0, y0, angle);

                for (double x = 0; x < source.Width; x += 1)
                {
                    for (int y = 0; y < source.Height; y++)
                    {
                        var X = CalculateNewX((int)x, y, cos, sin, x0, y0);
                        var Y = CalculateNewY((int)x, y, cos, sin, x0, y0);
                        X += offset.xOffset;
                        Y += offset.yOffset;

                        if (Y >= 0 && Y < newHeight && X >= 0 && X < newWidth)
                        {
                            var pixel = PixelOperations.GetPixelUnsafe(SrcPtr, SrcData.Stride, (int)Math.Round(x * 4), y, bytelength);
                            PixelOperations.SetPixelUnsafe(DestPtr, pixel, DestData.Stride, X * 4, Y, bytelength);
                            PixelOperations.SetPixelUnsafe(DestPtr, pixel, DestData.Stride, (X + 1) * 4, Y, bytelength);
                        }
                    }
                }
            }
            source.UnlockBits(SrcData);
            destination.UnlockBits(DestData);
            return(destination);
        }
Пример #3
0
        private void ZoomPicture(Bitmap source, Bitmap destination, double xZoom, double yZoom)
        {
            var initData   = source.LockBits(new Rectangle(0, 0, source.Width, source.Height), ImageLockMode.ReadOnly, source.PixelFormat);
            var destData   = destination.LockBits(new Rectangle(0, 0, destination.Width, destination.Height), ImageLockMode.WriteOnly, source.PixelFormat);
            int byteLength = 4;

            if (source.PixelFormat.ToString().Contains("24"))
            {
                byteLength = 3;
            }
            else if (source.PixelFormat.ToString().Contains("16"))
            {
                byteLength = 2;
            }
            // Get the address of the first line
            IntPtr ptr1 = initData.Scan0;
            IntPtr ptr2 = destData.Scan0;

            unsafe
            {
                byte *sourcePtr = (byte *)ptr1.ToPointer();
                byte *destPtr   = (byte *)ptr2.ToPointer();

                for (int i = 0; i < destData.Stride; i += 4)
                {
                    for (int j = 0; j < destination.Height; j++)
                    {
                        int newX = (int)(i / xZoom);
                        int newY = (int)(j / yZoom);
                        if (newX % byteLength != 0)
                        {
                            newX -= newX % byteLength;                         //offset to the first byte of four
                        }
                        var pixel = PixelOperations.GetPixelUnsafe(sourcePtr, initData.Stride, newX, newY, byteLength);
                        PixelOperations.SetPixelUnsafe(destPtr, pixel, destData.Stride, i, j, byteLength);
                    }
                }
            }

            source.UnlockBits(initData);
            destination.UnlockBits(destData);
        }