【问题标题】:Extracting a portion of Image in C#在 C# 中提取图像的一部分
【发布时间】:2013-10-08 15:27:20
【问题描述】:

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

【问题讨论】:

  • 声明“我想要 X”但没有显示尝试不是一个有效的问题。

标签: c#-4.0 asp.net-4.0


【解决方案1】:

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

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



    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-07-08
    • 2020-10-08
    • 2014-04-22
    • 2010-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多