5,661,954 members and growing! (14,465 online)
Email Password   helpLost your password?
Multimedia » General Graphics » Image Display     Intermediate License: The Code Project Open License (CPOL)

Application of the Zoomable and Scrollable Picturebox

By YLS CS

Testing a new image interpolation algorithm.
C#, Windows (WinXP, Windows), .NET, GDI+, Dev

Posted: 18 Aug 2008
Updated: 18 Aug 2008
Views: 5,178
Bookmarked: 13 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
11 votes for this Article.
Popularity: 3.56 Rating: 3.42 out of 5
2 votes, 18.2%
1
0 votes, 0.0%
2
2 votes, 18.2%
3
3 votes, 27.3%
4
4 votes, 36.4%
5

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)

About the Author

YLS CS



Location: United States United States

Other popular General Graphics articles:

  • A flexible charting library for .NET
    Looking for a way to draw 2D line graphs with C#? Here's yet another charting class library with a high degree of configurability, that is also easy to use.
  • CxImage
    CxImage is a C++ class to load, save, display, transform BMP, JPEG, GIF, PNG, TIFF, MNG, ICO, PCX, TGA, WMF, WBMP, JBG, J2K images.
  • 3D Pie Chart
    A class library for drawing 3D pie charts.
  • Really cool visual FX
    A set of classes for doing stunning visual effects, including water, plasma and fire.
  • ImageStone
    An article on a library for image manipulation.
Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 2 of 2 (Total in Forum: 2) (Refresh)FirstPrevNext
GeneralHey, really help full controlmemberSurajms19:49 27 Sep '08  
GeneralWhat is the interpolation algorithm?supporterMarc Clifton1:51 18 Aug '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 18 Aug 2008
Editor: Smitha Vijayan
Copyright 2008 by YLS CS
Everything else Copyright © CodeProject, 1999-2008
Web10 | Advertise on the Code Project