Pages by tag

34 pages are tagged with Coding Imaging

To check more tags, click here  click to open

Aspect correct resizing


You ever have one of those functions that you've created a hundred times in the past and every time you think you'll need it on a new project you say to yourself "I've done that a hundred times, no problem" then when you get around to typing it you sit there and scratch you head and say 'Now ... how do I do that again?" This is ours.

Use this function to resize an image to a specific width or height and keep the aspect ratio correct.

To cut down the "dots", the module or class or form holding the function needs the typical Drawing/Imaging namespaces...

Funky thumbnail images?


I've got a program that has to show image thumbnails. Everyone says to use GetThumbnailImage and it works great for some of the jpgs but others look terrible. They're all rippled around the edges of the different objects in the pictures. When I test using a regular picturebox with SizeMode = StretchImage and with the width and height of the control manually set to my thumbnail dimensions all of the images look fine.

Do the images that look wonky come from a digital camera?

I don't know, maybe. How can I tell?

Color to HEX


It's not a huge deal anymore to use Hex values for colors in web pages, since Netscape3+ and IE both handle basically the same set of user-friendly color text values, but sometimes we need to feel like getting geeky helps cover the bases.

As with most things on the sitelett, we found that we were typing this out by memory just often enough to make it tedious yet not often enough that our fingers could do it while we were watching TV. So we put it here for our future cut & pasting, and if it helps you too then so much the better.

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 3


Drag & drop loading is simple boilerplate code so it's very tempting but I'd say that usually you don't want to embed it into a control. If the dev wants to allow dragdrop on the control or on the entire form they can, using their own code they only need to pass the dropped filename to the LoadImage function. If you put the same code into your control then two loads will take place and two ImageLoaded events will fire. In the worst case a race condition could mess up their app and at least the dev and end user could get confused.

There's another reason embedding DragDrop isn't advised for controls: Exception handling. The events are being dynamically worked inside of the UserControl so the dev-user can't set a Try...Catch for them. That means when an invalid file is dropped and the LoadImage hits an exception it goes nowhere useful. It's a common quandary, typically the routes taken are: (1) Check the file during the DragEnter

Your own image control and App part 4


Just like most everything else in the runtime, Net's baked-in Exceptions are objects. As such they inherit from Object way up in their hierarchy, and closer to the bone they all inherit from System.Exception. While you can completely code your own exceptions from scratch with all your own syntax, it's most helpful for your user-devs if you make your custom exceptions do what the CLR ones do - inherit from System.Exception and expose the elements of the base interface that will primarily be used.

In the beginning days of .Net we were all urged to make our exceptions inherit from System.ApplicationException which in turn inherits from System.Exception. While many MS code samples show that being done, it didn't turn out to be of any use at all so now the general advice is to inherit from System.Exception directly. See this ...

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....

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