-1

I have made a C# Windows application with two Picture-Boxes one as MainPictureBox and other as ThumbnailBox, I want to Extract a portion of Image upon moving mouse over Main Image and load it into ThumbnailPictureBox.

1 Answers1

0

Here is a way to get a thumbnail from the image.

 public static Bitmap Thumb(this Image inputStream, int width, int height)
    {
        using (var bitMap = new Bitmap(inputStream))
        {
            int originalWidth = bitMap.Width;
            int originalHeight = bitMap.Height;
            int startPositionHeight = 0;
            int startPosionWidth = 0;
            int widthHeight = 0;

            if (originalWidth > originalHeight)
            {
                startPosionWidth = (originalWidth - originalHeight) / 2;
                widthHeight = originalHeight;
            }
            else if (originalHeight > originalWidth)
            {
                startPositionHeight = (originalHeight - originalWidth) / 2;
                widthHeight = originalWidth;
            }
            else if (originalWidth == originalHeight)
            {
                widthHeight = originalHeight;
            }

            var rect = new Rectangle(startPosionWidth, startPositionHeight, widthHeight, widthHeight);
            using (Bitmap cropped = bitMap.Clone(rect, bitMap.PixelFormat))
            {
                using (var newbitmap = new Bitmap(cropped, width, height))
                {
                    var stream = new MemoryStream();
                    newbitmap.Save(stream, ImageFormat.Tiff);
                    stream.Position = 0;

                    return new Bitmap(stream);
                }
            }
        }



    }
Filix Mogilevsky
  • 727
  • 8
  • 13