C# 中的图像缩放工具

发布于 2024-10-03 15:30:55 字数 2045 浏览 3 评论 0原文

我正在尝试制作一个轨迹栏,它将放大和缩小图片框中的图片。这是我当前的代码:

namespace Zoom_in_and_Out_Tool
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private Image imgOriginal;

        private void Form1_Load(object sender, EventArgs e)
        {
            // set image location
        imgOriginal = Image.FromFile(@"C:\New Folder\picture1.jpg");
        picBox.Image = imgOriginal;

        // set Picture Box Attributes
        picBox.BackgroundImageLayout = ImageLayout.Stretch;

        // set Slider Attributes
        zoomSlider.Minimum = 1;
        zoomSlider.Maximum = 5;
        zoomSlider.SmallChange = 1;
        zoomSlider.LargeChange = 1;
        zoomSlider.UseWaitCursor = false;

        // reduce flickering
        this.DoubleBuffered = true;
        }

        public Image PictureBoxZoom(Image img, Size size)
        {
        Bitmap bm = new Bitmap(img, Convert.ToInt32(img.Width * size.Width), Convert.ToInt32(img.Height * size.Height));
        Graphics grap = Graphics.FromImage(bm);
        grap.InterpolationMode = InterpolationMode.HighQualityBicubic;
        return bm;
        }

        private void zoomSlider_Scroll(object sender, EventArgs e)
        {
        if (zoomSlider.Value > 0)
            {
            picBox.Image = null;
            picBox.Image = PictureBoxZoom(imgOriginal, new Size(zoomSlider.Value, zoomSlider.Value));
            }
        }
    }
}

目前它出现了两个问题。一是它确实想使用 grap.InterpolationMode = InterpolationMode.HighQualityBicubic; 行进行编译。 。第二个问题是,当我尝试缩放时,会出现错误:““ArgumentException was unhandled”行错误:Bitmap bm = new Bitmap(img, Convert.ToInt32(img.Width * size.Width), Convert .ToInt32(img.Height * size.Height)); " 任何帮助都会很棒,

谢谢

更新 第一个错误表示:“名称‘InterpolationMode’在当前上下文中不存在” 当我注释掉这一行时,第二个错误是:“NullReferenceException 未处理”“对象引用未设置为对象的实例”。在线 Bitmap bm = new Bitmap(img, Convert.ToInt32(img.Width * size.Width), Convert.ToInt32(img.Height * size.Height));

谢谢

I am trying to make a trackbar which will zoom in and out on a picture in a picturebox. This is my current code:

namespace Zoom_in_and_Out_Tool
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private Image imgOriginal;

        private void Form1_Load(object sender, EventArgs e)
        {
            // set image location
        imgOriginal = Image.FromFile(@"C:\New Folder\picture1.jpg");
        picBox.Image = imgOriginal;

        // set Picture Box Attributes
        picBox.BackgroundImageLayout = ImageLayout.Stretch;

        // set Slider Attributes
        zoomSlider.Minimum = 1;
        zoomSlider.Maximum = 5;
        zoomSlider.SmallChange = 1;
        zoomSlider.LargeChange = 1;
        zoomSlider.UseWaitCursor = false;

        // reduce flickering
        this.DoubleBuffered = true;
        }

        public Image PictureBoxZoom(Image img, Size size)
        {
        Bitmap bm = new Bitmap(img, Convert.ToInt32(img.Width * size.Width), Convert.ToInt32(img.Height * size.Height));
        Graphics grap = Graphics.FromImage(bm);
        grap.InterpolationMode = InterpolationMode.HighQualityBicubic;
        return bm;
        }

        private void zoomSlider_Scroll(object sender, EventArgs e)
        {
        if (zoomSlider.Value > 0)
            {
            picBox.Image = null;
            picBox.Image = PictureBoxZoom(imgOriginal, new Size(zoomSlider.Value, zoomSlider.Value));
            }
        }
    }
}

Currently it comes up with 2 problems. One being it does want to compile with the line grap.InterpolationMode = InterpolationMode.HighQualityBicubic; . The Second problem is that when i try to zoom it comes up with the error: " "ArgumentException was unhandled" error at the line: Bitmap bm = new Bitmap(img, Convert.ToInt32(img.Width * size.Width), Convert.ToInt32(img.Height * size.Height)); " Any help would be great,

Thanks

UPDATE
The first error says: "The name 'InterpolationMode' does not exist in the current context"
The second error when i comment this line out is: 'NullReferenceException was unhandled "Object reference not set to an instance of an object.' on the line Bitmap bm = new Bitmap(img, Convert.ToInt32(img.Width * size.Width), Convert.ToInt32(img.Height * size.Height));

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

記柔刀 2024-10-10 15:30:55

包含

using System.Drawing.Drawing2D;

在您的使用列表中。

第二个错误可能是由于 img 为空或 size 为空造成的。

Include

using System.Drawing.Drawing2D;

in your using list.

The second error could be due to either the img being null or the size being null.

白衬杉格子梦 2024-10-10 15:30:55

第一个编译器错误很可能是由对 InterpolationMode.HighQualityBicubic 的未知引用引起的。 InterpolationMode 枚举位于 Drawing2D 命名空间中,该命名空间是 System.Drawing 的子命名空间。

您可以通过为 System.Drawing.Drawing2D 添加额外的 Using 指令,或者完全限定代码中的命名空间来修复此错误:

grap.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic;

您的代码的第二问题是您指定为此方法的参数的图像 (img) 是空引用。一旦创建控件(在表单的构造函数中),缩放滑块的 Scroll 事件可能就会引发,这是在表单的 Load 方法中的代码之前run,这就是创建图像的过程(通过从磁盘上的文件加载图像)。

尝试向 Scroll 事件处理程序添加 null 检查:

    private void zoomSlider_Scroll(object sender, EventArgs e)
    {
    if ((zoomSlider.Value > 0) && (imgOriginal != null))
        {
        picBox.Image = null;
        picBox.Image = PictureBoxZoom(imgOriginal, new Size(zoomSlider.Value, zoomSlider.Value));
        }
    }

最后,我注意到您正在设置图片框的 BackgroundImageLayout 属性,但您发布的代码实际上都没有指定图片框的背景图像。您是否想设置 SizeMode< /code> 属性来调整图像的显示方式?像这样的东西:

picBox.SizeMode = PictureBoxSizeMode.StretchImage;

The first compiler error is more than likely caused by an unknown reference to InterpolationMode.HighQualityBicubic. The InterpolationMode enumeration is found in the Drawing2D namespace, which is a child namespace of System.Drawing.

You can fix this error either by adding an additional Using directive for System.Drawing.Drawing2D, or by fully qualifying the namespace in your code:

grap.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic;

The second problem with your code is that the image you're specifying as a parameter to this method (img) is a null reference. The Scroll event of your zoom slider is probably getting raised as soon as the control is created (in your form's constructor), which is before the code in your form's Load method is run, which is what creates the image (by loading it from a file on disk).

Try adding a null check to the Scroll event handler:

    private void zoomSlider_Scroll(object sender, EventArgs e)
    {
    if ((zoomSlider.Value > 0) && (imgOriginal != null))
        {
        picBox.Image = null;
        picBox.Image = PictureBoxZoom(imgOriginal, new Size(zoomSlider.Value, zoomSlider.Value));
        }
    }

Finally, I noticed that you're setting the BackgroundImageLayout property of the picture box, but none of the code you post is actually specifying a background image for the picture box. Did you mean to set the SizeMode property to adjust how the image is displayed? Something like:

picBox.SizeMode = PictureBoxSizeMode.StretchImage;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文