Friday, October 29, 2004

Good to be pushed sometimes...

I sent the link to my birthday pics to a few friends of mine last night, and immediately JML got back to me via IM.

JML's a top guy. A real diamond. He's the IT manager where I used to work, and one of the only people from there whoI stil see regularly. Mrs Mawoo tells me he's the only person on the planet who I can talk to on the phone, as well.

So anyway, JML gets back to me immediately and says 'Can I offer some constructive criticism? Your pictures look shit. What interpolation did you use on them?' So I said I used the default compression, and promptly went out and tried to find out how I can apply a different algorithm to the images being shrunk (Curse you JML! I was about to go and watch some CSI!).

Good job I did, though, because I learnt a really cool, and quite fundamental thing.

System.Drawing.Graphics.FromImage() Doesn't give you a new blank canvas
Rather, it takes your blank canvas (or your canvas with an image on it) and allows you to use GDI+ to play about with it. Everything you do, though, every string you draw, every change you make, is made to the original image, not to a new one. To give an example:

Dim pic as Image
pic = pic.FromFile("C:\Monkey.jpg")
Dim little As New Bitmap(CInt(pic.Width / 4), CInt(pic.Height / 4))
Dim g As Graphics = Graphics.FromImage(little)
With g
.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
End With
g.DrawImage(pic, 0, 0, little.Width, little.Height)
little.Save("C:\SmallMonkey.jpg")


Now, yesterday, I would have said 'What? What are you doing saving a blank bitmap?'. That what it looked like to me. If you read through the code, it looks like the sequence of events is this:

  1. Open the original Image File
  2. Create a new blank Bitmap object 1/4 the size of the original
  3. Create a new Graphics Object, copying into it the blank cnvas you've just created
  4. Draw the image etc. on the new Graphics image, leaving the original blank bitmap (little) untouched
  5. Save the blank bitmap (little)
That's what it looked like to me, anyway.

I used to imaging the Graphics object as a posh bitmap, a bitmap tat you could do some whizzy things to, like write text on it and things like that. I now understand much differently. Rather than imagine the Graphics object as a blank canvas, I rather imagine it kind of like the team of robot arms that run around cars, welding bits together. You know the ones. When the car stops on the conveyor belt, and then a squad of robot arms comes out and welds, glues and paints everything before the car moves off again. That's how I visualize it now. Robot arms. With the car (or Bitmap, in this case) in the middle. Neato. And it answered a lot of questions.

This bit on MSDN helped me out a bit too.

It left me in a really good mood. For some sick reason, I actually get quite a buzz when I find out something new. I'm still really lazy, though. I wouldn't have played about with the Graphics object and found out what a cool thing it is without JML telling me 'Dude, your pictures are shit! Sort them out.' Thanks!

Just needed to share.

No comments: