Introduction
This program reads comic book files (cbr, cbz). These files are basically compressed jpeg picture files. I wrote this program to see if I could make a magnifying cursor. I think this would help others accomplish this task. I commented all the code; so even beginners could follow along. I have also included a comic book file in the downloads.
The steps to accomplish this are:
- Open a cbr or cbz file.
- Un-compress image files to a temp directory using a referenced DLL, I'm using SharpCompress which can be downloaded at http://sharpcompress.codeplex.com/.
- Read un-compressed image files into an image object and add the image object to a list of image objects, adding an
image
property called tag
for the image filename to each image object.
- Load the images into the GUI.
- I added a few nice features:
- When the user clicks the mouse down over the image, the cursor turns into a magnifying window.
- When the uses navigates to a new selection, the preview pane automatically syncs the scrollbar to bring the selection into view.
- The user can view two comic book pages when the zoom is set to 1.
- I demonstrate the use of the progress bar being updated on the form from a separate class.
Background
I used this site and many more on my image resizing method.
I used the SharpCompress
library to un-compress the rar and zip files.
I kept the program simple by only using one form.
I recently added the ability to view two pages at one time.
Using the Code
The method below (Figure 1) is called from the mousedown
and mousemove
events when the mouse is over the image. I grab a portion of the image under the mouse, I set the rectangle size of this portion based on the current image size and 20 pixels before the actual mouse point location.
I have to check my current mouse position and correct, so I do not grab anything that is beyond the boundaries of the image (this will cause an 'out of memory' error).
I send both the mouse-enter and the mouse-move events to this method, setting a flag on the mouse down event that it is OK to change the cursor. When the mouse-up event fires, the flag is set to false
, and the cursor is returned to normal.
Figure 1
private void Magnify(object sender, MouseEventArgs e, Image img)
{
try
{
Point point = new Point(e.X, e.Y);
X_RectSize = img.Width / 10;
Y_RectSize = img.Height / 20;
original = new Bitmap(img);
int coor_Y = e.Y - 20;
int coor_X = e.X - 20;
int coor_Xx = X_RectSize;
int coor_Yy = Y_RectSize;
if (coor_X < 0) coor_X = 0;
if (coor_Y < 0) coor_Y = 0;
if ((coor_X + coor_Xx) > img.Width)
{
coor_X = img.Width - X_RectSize;
}
if ((coor_Y + coor_Yy) > img.Height)
{
coor_Y = img.Height - Y_RectSize;
}
srcRect = new Rectangle(coor_X, coor_Y, coor_Xx, coor_Yy);
cropped = (Bitmap)original.Clone(srcRect, original.PixelFormat);
size1.Width = (coor_Xx * 3);
size1.Height = (coor_Yy * 2);
cropped = Utilities.ResizeImagePictureBox(cropped, size1, magnifyCursorType);
this.Cursor = new Cursor(cropped.GetHicon());
}
catch (Exception ex)
{
}
finally
{
original.Dispose();
cropped.Dispose();
}
}
The code in Figure 2 takes the bitmap portion/rectangle and enlarges it using the Size
object. I have included an option for a square or round appearance.
Figure 2
public static Bitmap ResizeImagePictureBox(Bitmap imgToResize, Size size, string cursorType)
{
Bitmap b = new Bitmap(size.Width, size.Height);
try
{
using (Graphics g = Graphics.FromImage((Image)b))
{
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
if (cursorType == "Round")
{
TextureBrush brush = new TextureBrush(imgToResize,
new Rectangle(0, 0, imgToResize.Width, imgToResize.Height));
brush.ScaleTransform(3, 3);
g.FillEllipse(brush, 0, 0, size.Width, size.Height);
}
else
{
g.DrawImage(imgToResize, 0, 0, size.Width, size.Height);
}
g.Dispose();
}
}
catch { }
return b;
}
Points of Interest
I made a comic book reader a few years back and had tried to magnify the cursor because I wanted to see the complete page when reading them. I was pretty busy at the time and failed in getting it working properly, and forgot about the project.
Recently, my stepson is interested in Comic Books and I failed to find my old code, so I decided to write another Comic Book reader. When I came to the point of magnification, I was destined this time to succeed.
This is the product of a weekend of off and on coding and experimenting with other snippets.
I am happy with the result of this magnifying example and feel it could be useful to others.
History
I recently added the ability to view two comic book pages at one time when the zoom level is set to 1.