Thursday, June 30, 2011

Creating Thumbnails C#

I was creating a website for a friend, and the photo gallery I decided to use needed thumbnails of all the pictures she had given me.  Now rather than do this by hand, or try and use the uploader I made on the site that can only handle three pictures at a time, I decided to make a program to do this for me.
First I create a folder in the path that was specified by the user, the string folder.  Then iterate on each file that is in the directory.
Directory.CreateDirectory(Path.Combine(folder, "thumbs"));
foreach (string pic in Directory.GetFiles(folder))
{
switch (Path.GetExtension(pic.ToLower()))
{
              case ".jpg": break;
case ".bmp": break;
case ".gif": break;
case ".exif": break;
case ".png": break;
case ".tiff": break;
default: // not a recognized format by
//System.Drawing.Bitmap so skip and go to the next
continue;
}
//more code to follow

I do some funky things to calculate the width and height that gets the images sized down to the way I want them.  I don’t think it’s really important and if you’re trying to implement this you should feel free to calculate the ratio any way you like.   Next, I create an empty bitmap to the height and width I want.  Then use the graphics class to load in the bitmap.  The settings SmoothingMode,  InterpolationMode, and PixelOffsetMode are all ones I found to keep the thumbnail looking good.  The last call with the graphics class, DrawImage is loading the image you want to resize onto the bitmap previously set up, shrinking it to fit, give you a nice thumbnail in memory.  The last thing to do is write the image to disc.  You can see in the code, I place the thumbnail in the thumbs directory previously created and use the name of the original file with thumb- prepended to the name.

using (Bitmap newImage = new Bitmap(newWidth, newHeight))
{
using (Graphics gr = Graphics.FromImage(newImage))
       {
              gr.SmoothingMode = SmoothingMode.AntiAlias;
              gr.InterpolationMode = InterpolationMode.HighQualityBicubic;
              gr.PixelOffsetMode = PixelOffsetMode.HighQuality;
              gr.DrawImage(new Bitmap(pic), new Rectangle(0, 0, newWidth, newHeight));
}

       string imgName = Path.GetFileName(pic); //get the images name
       //write the image to thumbs directory with the thumb-imageName
       newImage.Save(Path.Combine(folder, "thumbs\\thumb-" + imgName));
}
} //end foreach

That’s really all there is to it folks.  If you for some reason need to create a bunch of thumbnails and don’t want to implement it yourself, here is the executable I created, along with the source.

Friday, May 20, 2011

Why I've Been So Busy Pt2: Compilers

Took my a while to get time to write this up.  I've been busy moving stuff and getting back to work now that schools out.  Anyways, I took a compilers course last semester, it was a lot of work but it was fun and I learned a lot.  I mainly took it because of this blog post http://steve-yegge.blogspot.com/2007/06/rich-programmer-food.html that was in Hacker Monthly.  After reading it I was all about taking compilers.  Then a month went by and it was time to sign up for classes.  I almost didn't take compilers, in fact I had to read the article again to convince myself to.  I am glad I did.  I have a better understanding how they work, and what errors mean just to name a few things.

Anyways, in the class we made our own interpreter for a language KLOGO, specified by the professor.  It is a "logo" like language, in that it interprets your commands and moves a turtle around to draw a picture.  We used flex and bison for our parser/scanner respectively and the graphics professor at my school programed the graphics part we were to interface with.  After the scanning and parsing an AST was produced, that was type checked and finally executed the code.  It sounds pretty simple when I say it now, but it was a lot of work.  Below is an image of my interpreter interpreting a program I made.  It demonstrates most features of the interpreter.  Note: the output in the terminal was to demonstrate to the professor that it actually was working, and not just an image I drew.


Monday, May 9, 2011

Why I've Been So Busy Pt1: Ray Tracing

I've been super busy over the last semester.  The main reason would be my graphics class.  The main project is building a ray tracer.  Here are pictures from start to finish.

Circle Intersections Working

Triangle Intersection Working

Multiple Triangle Intersections

First 3D Object: Lambertian Shading

Multiple Sphere Intersection

1000 Spheres


Blinn-Phong Shading Working

Mirrors Working


Playing With Mirrored Spheres

First part of the project done (many hours later), next step bounding volume hierarchy, distributed rays, instancing and loading meshes. 

BVH working, 1 Million Spheres taking about 12 minutes, 10 depth reflections picture size 1000x1000

Anti-Aliasing


Image for Picture Contest

Spock



Soft Shadows and Instancing


Full Screen Photo Gallery - I made some of these to be wallpapers so feel free to use them as such.  Otherwise if you use them anywhere else just give me some credit.