Click here to Skip to main content
11,709,537 members (51,640 online)
Click here to Skip to main content

Application of the Zoomable and Scrollable Picturebox

, 17 Aug 2008 CPOL 28.6K 1.5K 37
Rate this:
Please Sign up or sign in to vote.
Testing a new image interpolation algorithm.

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:

Selection selection;

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

and two public methods:

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:

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.

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)

Share

About the Author

YLS CS
Unknown
No Biography provided

You may also be interested in...

Comments and Discussions

 
GeneralMy vote of 5 Pin
aalhanane6-Jun-13 2:36
memberaalhanane6-Jun-13 2:36 
GeneralHey, really help full control Pin
Surajms27-Sep-08 18:49
memberSurajms27-Sep-08 18:49 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150819.1 | Last Updated 18 Aug 2008
Article Copyright 2008 by YLS CS
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid