1

Goal:

My current goal is to crop the original image from pictureBox1 and show it in pictureBox2 and increase it's size (Height & Width) by 0.8.

Current Code:

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

Cropping the image:

//...
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;
}
//...

Current code - what does it do

Currently this code is able to mark an red area in pictureBox1 and show the cropped image in pictureBox2.

Question:

How do I increase/resize the cropped image height and width by 0.8?

Eduards
  • 1,734
  • 2
  • 12
  • 37
  • Does this answer your question? [How to resize an Image C#](https://stackoverflow.com/questions/1922040/how-to-resize-an-image-c-sharp) – Eliahu Aaron May 28 '20 at 13:21
  • @JQSOFT that did work but hat convert from double to int. I'll now need to get a better quality image. – Eduards May 28 '20 at 13:44

2 Answers2

1

Calculate the ratio of the scale up against the source size and use it to calculate the new size of the image. Accordingly, the pictureBox2.Paint event code:

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

To apply that on the GetCroppedImage() function:

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

Also, you could use the Image.GetThumbnailImage to get small sizes from the main image. Not recommended if you need to return large ones. Read the Remarks for more information.

  • If I were a manager I would ;) - I am just curious, you seem very intelligent in your area! I think you can teach some valuable lessons. – Eduards May 29 '20 at 13:47
0
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;
}

Usage:

ResizeImage(originalImg, (int)(originalImg.Width * 0.8), (int)(originalImg.Height * 0.8));
Abhay
  • 174
  • 1
  • 7