-1

我已经制作了一个带有两个图片框的 C# Windows 应用程序,一个作为 MainPictureBox,另一个作为 ThumbnailBox,我想在将鼠标移到主图像上时提取图像的一部分并将其加载到 ThumbnailPictureBox。

4

1 回答 1

0

这是一种从图像中获取缩略图的方法。

 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);
                }
            }
        }



    }
于 2013-10-08T19:21:23.033 回答