Question

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

Was it helpful?

Solution

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.

OTHER TIPS

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;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top