Search code examples
c#zoomingpictureboxtrackbar

Image Zoomer tool in c#


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


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.