Click here to Skip to main content
15,886,362 members
Articles / Multimedia / GDI+
Article

Application of the Zoomable and Scrollable Picturebox

Rate me:
Please Sign up or sign in to vote.
4.07/5 (13 votes)
17 Aug 2008CPOL2 min read 44K   2K   41   3
Testing a new image interpolation algorithm.

Image 1

Introduction

There are many approaches for image enlargement. This program was written to test a newly developed image interpolation algorithm.

How to use this program

To use this program, you first have to open a picture using the Open button. After you open the picture of your choice, the picture will appear in the left picture box. In this box, you would see a white rectangle. This shows the part of the original picture that appears in the right picture box. The picture in the right picture box can be resized using the zoom track bar. You can also change the sharpness using the sharpness track bar to make the picture clearer. After you have the desired zoom and proper sharpness, you save the picture using the Save button. The larger the zoom factor, the longer it will take. The newly saved enlarged picture will have much sharper results, but still maintains the original details.

About this program

This program includes the Zoomable and Scrollable Picturebox, which is described on CodeProject. Some members suggested this user control should include crop and selection of images. I took the suggestions into consideration and came up with this program. In this program, I have added a property for the Zoomable and Scrollable Picturebox control:

C#
Selection selection;

public Selection Selection
{
    get { return selection; }
    set { selection = value; Invalidate(); }
}

and two public methods:

C#
public Point ConvertControlPointToCanvas(Point point)
{
    Point pt = new Point();
    if (viewRectWidth > canvasSize.Width * zoom)
    {
        pt.X = (int)((float)(point.X - viewRectWidth / 2 + 
                canvasSize.Width * zoom / 2f) / zoom);
        pt.X = Math.Min(Math.Max(pt.X, 1), canvasSize.Width - 1);
    }
    else pt.X = (int)((float)(point.X + hScrollBar1.Value) / zoom);
    if (viewRectHeight > canvasSize.Height * zoom)
    {
        pt.Y = (int)((float)(point.Y - viewRectHeight / 2 + 
                canvasSize.Height * zoom / 2f) / zoom);
        pt.Y = Math.Max(Math.Min(pt.Y, canvasSize.Height - 1), 1);
    }
    else pt.Y = (int)((float)(point.Y + vScrollBar1.Value) / zoom);
    return pt;
}

public Point ConvertCanvasPointToControl(Point point)
{
    float xOffset = viewRectWidth > canvasSize.Width * zoom ? 
          (viewRectWidth - canvasSize.Width * zoom) / 2f : -hScrollBar1.Value;
    float yOffset = viewRectHeight > canvasSize.Height * zoom ? 
          (viewRectHeight - canvasSize.Height * zoom) / 2f : -vScrollBar1.Value;
    Matrix mxCanvastoContol = new Matrix();
    mxCanvastoContol.Scale(zoom, zoom);
    mxCanvastoContol.Translate(xOffset, yOffset, MatrixOrder.Append);
    Point[] pts = new Point[] { point };
    mxCanvastoContol.TransformPoints(pts);
    return pts[0];
}

I have also written a class selection.cs for the Selection property:

C#
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;


namespace YLScsZoom
{
    public class Selection
    {
        Color lineColor = Color.Black;
        float lineWidth = 1.0f;

        Point location = new Point(0, 0);
        Size size = new Size(0, 0);

        public Color LineColor
        {
            get { return lineColor; }
            set { lineColor = value; }
        }

        public float LineWidth
        {
            get { return lineWidth; }
            set { lineWidth = value; }
        }

        public Size Size
        {
            get { return size; }
            set { size = value; }
        }

        public Point Location
        {
            get { return location; }
            set { location = value; }
        }

        public void Draw(Graphics g)
        {
            Pen p = new Pen(lineColor, lineWidth);
            g.DrawRectangle(p, new Rectangle(location, size));
            p.Dispose();
        }

        public virtual bool isHitting(Point pt)
        {
            Rectangle r = new Rectangle(location, size);
            if (r.Contains(pt))
                return true;
            else return false;
        }
    }
}

You can select an image with selection through its properties Location and Size. In this program, I have used the mouse events of the control to get the point in the control, then converted it to an image, which is the selection Location, by using the method ConvertControlPointToCanvas. The selection Size is determined by the zoom factor and the size of the client window that shows the enlarged image.

The image interpolation approach used in this program is totally different from the methods mentioned by Libor Tinka. Mine has two parameters: zoom factor and sharpness factor.

C#
using System;
using System.Drawing;

namespace YLScsLib.Imaging
{
    public class YLScsZoom
    {
        public YLScsZoom();

        public static Bitmap Apply(Bitmap srcBmp, float zoom);
        public static Bitmap Apply(Bitmap srcBmp, float zoom, float factor);
        public static byte[,] Apply(byte[,] srcBytes, 
               int nwidth, int nheight, float factor);
        public static ChannelBytes Apply(ChannelBytes srcBytes, 
               int nwidth, int nheight, float factor);
    }
}

Their values are provided by the two track bars in this program.

Thanks

Thanks a lot for trying this program and my image interpolation algorithm.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Unknown
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 5 Pin
aalhanane6-Jun-13 2:36
aalhanane6-Jun-13 2:36 
GeneralHey, really help full control Pin
Surajms27-Sep-08 18:49
Surajms27-Sep-08 18:49 
QuestionWhat is the interpolation algorithm? Pin
Marc Clifton18-Aug-08 0:51
mvaMarc Clifton18-Aug-08 0:51 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.