Lossless Image Resizing in C# - Let's Resize!
(Page 4 of 6 )
We have finally covered the quantization process and we can theoretically perform lossless conversion and save images, excluding the GIF limitations. We cannot violate the nature of GIFs or spend countless hours developing ourselves a better quantization algorithm - GIFs are obsolete anyway, so let's get over it! Now it's time to architect and implement the resizing function into our application.
First of all, as you'd expect, we are going to add new components to our form. I for one have added the following new components: another button called btnResize, two textboxes called txtHeight and txtWidth, a few additional labels such as lbResize, lblRatio, lbl_x (check the screenshot attached below to understand), and of course, another piece of saveFileDialog2, which is required when we want to save our destination file.
As I mentioned, check out the screenshot below - it should definitely help you.

(Form Design - Screenshot)
The rest remained untouched, such as picThumbnail, btnQuit, btnSelectPicture, btnConvert, and of course the form remained clsMain. All that is left to modify is to Show and Hide the appropriate labels and buttons when needed. This means that before we load a picture, we hide all of the components excluding two buttons (Quit and Select Picture) and the picThumbnail. After we've chosen the source image, voila, the rest of the components also appear. Now we'll have a powerful application.
I won't attach code snippets for the additions I described above because it's quite self-explanatory and it was explained thoroughly during the first part of this series. And even if you cannot do this, the components will always be visible - it simply doesn't look professional enough. We can afford that kind of possibility.
Now let's write the resizing algorithms! We need to code the btnResize_Click and saveFileDialog2_FileOK methods. However, we are going to create another method as well as a new function. These are going to act as the foundation for our resizing action. These are ResizeImage() and GetDimensions. The first handles the resizing process while the latter calculates the new dimensions, because we maintain proportionality (ratio).
public void ResizeImage(int MaxWidth, int MaxHeight, string FileName, string NewFileName, string NewFileExtension)
{
Bitmap OrigImg = (System.Drawing.Bitmap)Image.FromFile(FileName).Clone();
Size ResizedDimensions = GetDimensions(MaxWidth, MaxHeight, ref OrigImg);
Bitmap NewImg = new Bitmap(OrigImg, ResizedDimensions);
saveFile(NewImg, NewFileName, NewFileExtension);
}
The above code snippet is our ResizeImage method. It clones the source image and then calls our next-to-be-written GetDimensions function to calculate the new, resized dimensions. After these we can finally call our saveFile method, which remained untouched since the quantization changes (previous page). So let's see GetDimensions!
public static Size GetDimensions(int MaxWidth, int MaxHeight, ref Bitmap Img)
{
int Height; int Width; float Multiplier;
Height = Img.Height; Width = Img.Width;
if (Height <= MaxHeight && Width <= MaxWidth)
return new Size(Width, Height);
Multiplier = (float)((float)MaxWidth / (float)Width);
if ((Height * Multiplier) <= MaxHeight)
{
Height = (int)(Height * Multiplier);
return new Size(MaxWidth, Height);
}
Multiplier = (float)MaxHeight / (float)Height;
Width = (int)(Width * Multiplier);
return new Size(Width, MaxHeight);
}
Maybe it isn't as straightforward as I had hoped. Anyway, I'm going to explain how it works. Basically, we want to preserve proportionality (ratio). At first, we check whether the new sizes aren't smaller than the original ones (in this case, we return the original ones and basically do zero resizing). If this isn't the case, then we calculate the MaxWidth and Width ratio - basically MaxWidth stands for the explicitly specified width.
After this, we're able to see whether we need to fiddle with the height of the image. In that case, we resize it using the multiplier we've just calculated to maintain the proportionality. Of course, after this we also need to reduce the size of the width, so we calculate the MaxHeight and Height ratio to accomplish this. Once all of this is done, we end this function by returning our new size.
Next: Completing the Project >>
More C# Articles
More By Barzan "Tony" Antal