如何调整裁剪图像的尺寸?

时间:2020-05-28 13:13:48

标签: c# picturebox system.drawing

目标:

我当前的目标是从pictureBox1裁剪原始图像并在pictureBox2中显示,并将其大小(高度和宽度)增加0.8

当前代码:

//...
private Point LocationXY;
private Point LocationX1Y1;

private void button1_Click(object sender, EventArgs e)
{
    if (Clipboard.ContainsImage())
    {
        pictureBox1.Image?.Dispose();
        pictureBox1.Image = Clipboard.GetImage();
    }
}

private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
    if(e.Button == MouseButtons.Left)
        LocationXY = e.Location;
}

private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.Left)
    {
        LocationX1Y1 = e.Location;
        pictureBox1.Invalidate();
    }
}

private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
    if(e.Button == MouseButtons.Left)
    {
        LocationX1Y1 = e.Location;

        pictureBox1.Invalidate();
        pictureBox2.Invalidate();
    }
}

private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
    if (MouseButtons == MouseButtons.Left)
        e.Graphics.DrawRectangle(Pens.Red, GetRect());
}

private void pictureBox2_Paint(object sender, PaintEventArgs e)
{
    var src = GetRect();

    if (src == Rectangle.Empty) return;

    var des = new Rectangle(0, 0, src.Width, src.Height);

    e.Graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
    e.Graphics.SmoothingMode = SmoothingMode.HighQuality;
    e.Graphics.CompositingQuality = CompositingQuality.HighQuality;

    e.Graphics.DrawImage(pictureBox1.Image,
        des, src, GraphicsUnit.Pixel);
}

private Rectangle GetRect()
{
    return new Rectangle(
        Math.Min(LocationXY.X, LocationX1Y1.X),
        Math.Min(LocationXY.Y, LocationX1Y1.Y),
        Math.Abs(LocationXY.X - LocationX1Y1.X),
        Math.Abs(LocationXY.Y - LocationX1Y1.Y)
        );
}
//...

裁剪图像:

//...
private Bitmap GetCroppedImage()
{
    var src = GetRect();

    if (src == Rectangle.Empty) return null;

    var des = new Rectangle(0, 0, src.Width, src.Height);
    var b = new Bitmap(src.Width, src.Height);

    using (var g = Graphics.FromImage(b))
    {
        g.InterpolationMode = InterpolationMode.HighQualityBicubic;
        g.SmoothingMode = SmoothingMode.HighQuality;
        g.CompositingQuality = CompositingQuality.HighQuality;

        g.DrawImage(pictureBox1.Image, des, src, GraphicsUnit.Pixel);
    }
    return b;
}
//...

当前代码-它的作用

当前,此代码能够在pictureBox1中标记红色区域,并在pictureBox2中显示裁剪的图像。

问题:

如何将裁剪后的图像高度和宽度按0.8增加/调整大小?

2 个答案:

答案 0 :(得分:1)

计算缩放比例与源尺寸的比例,并使用它来计算图像的新尺寸。因此,pictureBox2.Paint事件代码:

private void pictureBox2_Paint(object sender, PaintEventArgs e)
{
    var src = GetRect();

    if (src == Rectangle.Empty || src.Width == 0 || src.Height == 0) return;

    e.Graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
    e.Graphics.SmoothingMode = SmoothingMode.HighQuality;
    e.Graphics.CompositingQuality = CompositingQuality.HighQuality;

    var widthScale = 0.8;
    var heightScale = 0.8;
    var widthRatio = (src.Width * widthScale + src.Width) / src.Width;
    var heightRatio = (src.Height * heightScale + src.Height) / src.Height;
    var newWidth = (int)(src.Width * widthRatio);
    var newHeight = (int)(src.Height * heightRatio);
    var des = new Rectangle(0, 0, newWidth, newHeight);

    e.Graphics.DrawImage(pictureBox1.Image,
        des, src, GraphicsUnit.Pixel);
}

要将其应用于GetCroppedImage()函数:

private Image GetCroppedImage(double scale = 0)
{
    var src = GetRect();

    if (src == Rectangle.Empty || src.Width == 0 || src.Height == 0) return null;

    var widthRatio = (src.Width * scale + src.Width) / src.Width;
    var heightRatio = (src.Height * scale + src.Height) / src.Height;
    var newWidth = (int)(src.Width * widthRatio);
    var newHeight = (int)(src.Height * heightRatio);

    if (newWidth == 0 || newHeight == 0) return null;

    var des = new Rectangle(0, 0, newWidth, newHeight);
    var b = new Bitmap(newWidth, newHeight);

    using (var g = Graphics.FromImage(b))
    {
        g.InterpolationMode = InterpolationMode.HighQualityBicubic;
        g.SmoothingMode = SmoothingMode.HighQuality;
        g.CompositingQuality = CompositingQuality.HighQuality;
        g.DrawImage(pictureBox1.Image, des, src, GraphicsUnit.Pixel);
    }

    return b;
}

此外,您可以使用comment从主图像中获取较小的尺寸。如果您需要退货,则不建议使用。阅读Image.GetThumbnailImage了解更多信息。

答案 1 :(得分:0)

int counter = (int)stream.filter(Objects::nonNull)
                         .filter(c -> c.contains("something")) // or single filter with c -> c != null && c.contains("something") as pred
                         .count();

用法:

public static Bitmap ResizeImage(System.Drawing.Image image, int width, int height)
{
    var destRect = new Rectangle(0, 0, width, height);
    var destImage = new Bitmap(width, height);

    destImage.SetResolution(image.HorizontalResolution, image.VerticalResolution);

    using (var graphics = Graphics.FromImage(destImage))
    {
        graphics.CompositingMode = CompositingMode.SourceCopy;
        graphics.CompositingQuality = CompositingQuality.HighQuality;
        graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
        graphics.SmoothingMode = SmoothingMode.HighQuality;
        graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;

        using (var wrapMode = new ImageAttributes())
        {
            wrapMode.SetWrapMode(WrapMode.TileFlipXY);
            graphics.DrawImage(image, destRect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, wrapMode);
        }
    }

    return destImage;
}