Pages by tag

28 pages are tagged with Coding VB Imaging

To check more tags, click here  click to open

Your own image control and App part 1


Let's imagine we're in an end user planning session, at the point where all of the company gossip is over and the brown dribbles have dried down the sides of the cold coffee cups. The five real minutes of work in that hour and a half meeting boiled down to the following functional spec:

  1. We need to have a program that lets us load various image files including JPEGs, BMPs, PNGs, GIFs and "Faxes" ("faxes" are always thrown in when we're not listening closely)
  2. We need to be able to rotate the images.
  3. We need to be able to zoom in and out of the images.
  4. We need to be able to select parts of an image and save just the selection as a new image ("crop").
  5. We need to save the rotated/cropped image out to a new image file with various file type options.
  6. We need it done two weeks ago or the company will go under and we'll have to move to India to beg for coins from the only people left in the world doing real tech work.

Your own image control and App part 2


Setting up the workspace

Load Visual Studio and on the Start page, click on the "New Project" button. In the top left "Project Types" tree, select "Visual Basic Projects" and in the right "Templates" pane choose the "Windows Control Library" template. Down at the bottom of the screen, name the project "svImageEdit" and press OK.

This next part may seem a little odd, just trust me. When the project is ready, right click on the Usercontrol.vb node of the Solution Explorer tree and click on "Delete" to kill the default file. Now right click on the project node and on the popup click on "Properties" and in the popup set the "Default Namespace" to "Smithvoice" (You can use the name of your company or organization, just remember to use your value where I type "Smithvoice" later in the code). Press OK to close the project properties form. Right click again on the project node and click on the popup's "Add User Control" option and use the wizard to create a new UC named "svImageEditor".

Your own image control and App part 5


If you tested the image loading with a "fax image" *.tif file (or *.tiff" file) you probably got it to display... sort of. Faxes received by computers are traditionally held in TIF files, but TIFs aren't exactly Image files in the manner of *.bmp, *.gif and *.jpg/*.jpeg.

To get at the collection of a TIF's images back in the VBCom days the path of least grief was to buy into a 3rd party tool such as LeadTools, but .Net makes working with TIFs a lot easier. Now, with our own GDI+ and System.Drawing code we can not only read the images but also add, delete and edit a file's internal image collection.

Your own image control and App part 6


Custom events:

Switch mental gears for a second and put yourself in the role of dev-user. When you load a file that contains multiple images you're probably going to want to show the count to your end users right away. You can do this with the current code by remembering to check the TotalPageCount property but it would be nicer if there was an event you could trap, so we'll add one called "ImageLoaded" that has the count as one of its arguments.

Custom events have come a long way from the VBClassic days, now we have lots of options including tooling everything out with Delegates, wiring and unwiring events dynamically at runtime and even just forgetting we're in .Net and doing it "The VB Way". We're going to use a hybrid style to get the advantages of both VB and .Net.

Your own image control and App part 7


Selecting specific fax pages

The comments tell most of the story and to answer the obvious question, a multipage image file doesn't do anything itself to help you show the internal images, it's just a container and you have to remember what page you're working with. To make a particular page active you specify the zero-based value in a call to [Image].SelectActiveFrame then, if showing the image as we are here, you reload it into the display widget.

The Set raises an ArgumentOutOfRangeException if the specified value makes no sense for the file (Remember, it's not advised to make a custom exception if a built-in exception easily covers the situation). If the Set works a new custom "PageChanged" event is raised to allow the dev-user to wire up their GUIs....

Your own image control and App part 8


Feature 2: Rotating image displays

There are two main ways to rotate graphics with .Net, [Graphics].RotateTransform and [Image].RotateFlip.

[Graphics].RotateTransform is very powerful and with it you can rotate an image by any amount of degrees. With its power, of course, comes extra work and having to add support to eliminate assumptions (such as: when the resulting bitmap exposes areas that aren't filled in by the original image data, how does the user want those areas filled in?). If we were creating a full image editing program to go up against PaintShopPro then we would be very happy to have this big gun at our fingertips.

In our situation a simple way to transform images by 90 degree increments is really all that's needed and that need is quickly and easily taken care of using [Image].RotateFlip set to one of the many built-in System.Drawing.Imaging.RotateFlipType enumeration values.

Your own image control and App part 9


The most useful tool in GDI+: DrawImage

There are a number of ways to make a pixel for pixel copy of a raster graphic. As we've seen, applying RotateFlip is one way but it doesn't help anyone deal with multipage tifs. Some of the other techniques include:

- Create a new bitmap object and loop through each individual pixel in the source graphic using GetPixel to read the pixel data and SetPixel to copy it to the same X/Y location of the destination bitmap. Even on a fast computer this can be very slow process and is best used when you have to perform some manipulation to each pixel such as altering the RGB value for a color adjustment.

- P/Invoke ("Platform...

Your own image control and App part 10


Feature 3: Zooming

"Zooming" is really just making an image bigger or smaller. If it's too big to fit the picturebox area then it appears that you're seeing it close-up and if it takes up just a portion of the picturebox then it looks far away. Simple enough, "zooming" means resizing.

You've already figured that the same types of options for rotating apply to zooming, we'll just agree that while it can be done with Win32 it's best to stick with a ".Net way" ... but it can also be done "The VB way".

.Net's DrawImage has a lot of overloaded versions including ones that let you squish or expand the original bits to a differently sized destination bitmap. If you do this with a good enough interpolation mode you can get very nice results but if your users like to play with their buttons there is a chance of degradation over time.

Your own image control and App part 11


Handling the unhandleable exception

Even though the message calls this an "unhandled exception", the truth is that it's unhandle-able. If you step through the code you'll easily track down where it happens (in UpdateZoomDisplay right after you change the picturebox size) but you'll also see that the current catch-all exception handler is getting the message so adding a catch for System.ComponentModel.Win32Exception also won't work. Plus, you have a catch-all down the stack in the testHarness's button click event and it's not recognizing the exception either.

To quickly see the stack dump, play end-user on a debug build. Rebuild the solution and run the exe outside of Visual Studio then zoom till lightning strikes:

Your own image control and App part 12


Fixing the squish

Now that you can see whole images, the oddly sized fax pages are probably bothering the heck out of you. The reason for the "squishes" is the Fax transmission compression.

When you send a fax you can specify different quality/speed settings typically called "Draft" and "High"; Different machines allow different settings but these two are usually in the list somewhere. The difference is the image resolution. Along with converting full color sources to 1bitperpixel black and white format, fax compression traditionally trims down the number of bits by reducing the number of vertical pixels. A rule of thumb way to think of it is that


<< 1 2 3 >> 
who is smith     contact smith     rss feed
Since 1997 a place for my stuff, and it if helps you too then all the better