如何在strechmode-C中在图片框中裁剪和保存图像# [英] How to crop and save an image in picturebox in strechmode-C#

查看:90
本文介绍了如何在strechmode-C中在图片框中裁剪和保存图像#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I am unable to crop an image in a picturebox in strechmode which i tried for weeks and dint get a solution yet. The problem is my images size is say(1200,750) and the picture size is say(450,300) but when i try to crop it in picturebox in strechmode it takes the coordinates of original size of the image(1200,750) but i need to crop with the coordinates of the image displayed in picturebox(450,300), therefore i am getting faulty results in cropping. I am working with visual studio 2013 in c# winforms. Please provide me with a solution. Thank you in advance.





我尝试过:





What I have tried:

private void pictureBox1_MouseDown(object sender, MouseEventArgs e) { // Starting point of the selection: if (e.Button == MouseButtons.Left) { _selecting = true; _selection = new Rectangle(new Point(e.X, e.Y), new Size()); } }

    private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
    {
        // Update the actual size of the selection:
        if (_selecting)
        {
            _selection.Width = e.X - _selection.X;
            _selection.Height = e.Y - _selection.Y;

            // Redraw the picturebox:
            pictureBox1.Refresh();
        }
    }

    private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
    {
        // Make a note that we "have the mouse". 
        bHaveMouse = true;

        // Store the "starting point" for this rubber-band rectangle. 
        ptOriginal.X = e.X;
        ptOriginal.Y = e.Y;

        // Special value lets us know that no previous 
        // rectangle needs to be erased. 

        // Display coordinates 
        //lbCordinates.Text = "Coordinates  :  " + e.X.ToString() + ", " + e.Y.ToString();

        //ptLast.X = -1;
        //ptLast.Y = -1;

        //rectCropArea = new Rectangle(new Point(e.X, e.Y), new Size()); 
        if (e.Button == MouseButtons.Left && _selecting)
        {
            // Create cropped image:
            Image img = pictureBox1.Image.Crop(_selection);

            // Fit image to the picturebox:
            pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
            pictureBox1.Image = img.Fit2PictureBox(pictureBox1);
            _selecting = false;
        }
        button5.Enabled = true;
}

    private void pictureBox1_Paint(object sender, PaintEventArgs e)
    {
        if (_selecting)
        {
            // Draw a rectangle displaying the current selection
            Pen pen = Pens.GreenYellow;
            e.Graphics.DrawRectangle(pen, _selection);
        }
    }
the namespace class i used is as followsnamespace gfoidl.Imaging {

public static class ImageExtension
{

    public static Image Crop(this Image image, Rectangle selection)
    {
        Bitmap bmp = image as Bitmap;

        // Check if it is a bitmap:
        if (bmp == null)
            throw new ArgumentException("Kein gültiges Bild (Bitmap)");

        // Crop the image:
        Bitmap cropBmp = bmp.Clone(selection, bmp.PixelFormat);

        // Release the resources:
        image.Dispose();

        return cropBmp;
    }

    public static Image Fit2PictureBox( this Image image, PictureBox picBox)
    {
        Bitmap bmp = null;
        Graphics g;

        // Scale:
        double scaleY = (double)image.Width / picBox.Width;
        double scaleX = (double)image.Height / picBox.Height;
        double scale = scaleY < scaleX ? scaleX : scaleY;

        // Create new bitmap:
        bmp = new Bitmap(
            (int)((double)image.Width / scale),
            (int)((double)image.Height / scale));

        // Set resolution of the new image:
        bmp.SetResolution(
            image.HorizontalResolution,
            image.VerticalResolution);

        // Create graphics:
        g = Graphics.FromImage(bmp);

        // Set interpolation mode:
        //g.InterpolationMode = InterpolationMode.HighQualityBicubic;

        // Draw the new image:

        g.DrawImage(
            image,
            new Rectangle(          // Ziel
                0, 0,
                bmp.Width, bmp.Height),
            new Rectangle(          // Quelle
                0, 0,
                image.Width, image.Height),
            GraphicsUnit.Pixel);
        picBox.SizeMode = PictureBoxSizeMode.StretchImage;

        // Release the resources of the graphics:
        g.Dispose();

        // Release the resources of the origin image:
        image.Dispose();

        return bmp;
    }
}
}` invoked it in the main program with the header using gfoid1.imaging;

推荐答案

参见这个 Stackoverflow线程。



希望它可以帮到你。
See this Stackoverflow thread.

Hope it helps you.


这篇关于如何在strechmode-C中在图片框中裁剪和保存图像#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆