Scott Hanselman

Scaling/Resizing/Resampling an Image in ASP.NET

June 09, 2004 Comment on this post [10] Posted in ASP.NET | DasBlog
Sponsored By

A friend was an image in C# yesterday, and was commenting on how crappy the output looked.  His code was very similar to the code in dasBlog that resizes an image when an entry is email-to-blog'ed.

/// Copyright (c) 2003, newtelligence AG. (http://www.newtelligence.com)
/// <summary>

/// This function is used for thumbnailing and gets an image encoder
/// for a given mime type, such as image/jpeg
/// </summary>
/// <param name="mimeType"></param>
/// <returns></returns>
private ImageCodecInfo GetEncoderInfo(string mimeType)
{
ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();
foreach (ImageCodecInfo codec in codecs)
{
if (codec.MimeType == mimeType)
{
return codec;
}
}
return null;
}

<SNIPPET>

string absoluteFileName = Path.Combine(binariesPath, fileName);
string thumbBaseFileName = Path.GetFileNameWithoutExtension(fileName)+"-thumb.dasblog.JPG";
string thumbFileName = Path.Combine(binariesPath, thumbBaseFileName);
Bitmap sourceBmp = new Bitmap(absoluteFileName);
if ( sourceBmp.Height > siteConfig.Pop3InlinedAttachedPicturesThumbHeight )
{
Bitmap targetBmp = new Bitmap(sourceBmp,new Size(
Convert.ToInt32(Math.Round((((double)sourceBmp.Width) * (((double)siteConfig.Pop3InlinedAttachedPicturesThumbHeight) / ((double)sourceBmp.Height))),0)),
siteConfig.Pop3InlinedAttachedPicturesThumbHeight));

ImageCodecInfo codecInfo = GetEncoderInfo("image/jpeg");
Encoder encoder = Encoder.Quality;
EncoderParameters encoderParams= new EncoderParameters(1);
long compression=75;
EncoderParameter encoderParam = new EncoderParameter(encoder,compression);
encoderParams.Param[0] = encoderParam;
targetBmp.Save(thumbFileName,codecInfo,encoderParams);

string absoluteUri = new Uri( binariesBaseUri, fileName ).AbsoluteUri;
string absoluteThumbUri = new Uri( binariesBaseUri, thumbBaseFileName ).AbsoluteUri;
entry.Content += String.Format("<div class=\"inlinedMailPictureBox\"> <a href=\"{0}\"><img border=\"0\" class=\"inlinedMailPicture\" src=\"{2}\"></a> <br> <a class=\"inlinedMailPictureLink\" href=\"{0}\">{1}</a></div>",absoluteUri, fileName, absoluteThumbUri);
scalingSucceeded = true;

</SNIPPET>

This code (above) works fine for extracting an image from an email and scaling (thumbnailing) it, but the result is fairly jaggy, and one would certainly expect better resampling than is seen in the image above.

I found this article on DevEx that seemed to offer these lines as a possible solution:

    g.SmoothingMode =SmoothingMode.HighQuality;
    g.InterpolationMode =InterpolationMode.HighQualityBicubic;
    g.PixelOffsetMode =PixelOffsetMode.HighQuality;

But there doesn't appear to be any visible change (to my eye) with these added.  Should the SmoothingMode be set to 'SmoothingModeAntiAlias' instead?  Smooth resampling of an image in C# certainly seems it should be a 'solved problem.'  Perhaps I'm not Googling well.

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook bluesky subscribe
About   Newsletter
Hosting By
Hosted on Linux using .NET in an Azure App Service

The SQL Queen, Kimberly Tripp, is blogging!

June 09, 2004 Comment on this post [0] Posted in Programming
Sponsored By

Everyone once in a while someone says "So-and-so is blogging" and then a whoop goes up in the Blogosphere as folks update their blog rolls and link around. 

Often starts with a post like (this reads best when you do an impression of Rick Moranis in Ghostbusters):

Well...hey...uh...everyone else is blogging, so here's mine.  I plan many good things.  I hope to see you around here.  Here's a picture of my cat.

But not Kimberly Tripp.  Not only has she started a blog, but she's already populating it with good content.  Here's a gem: Stored Procs are NOT evil.

Enjoy...I'm sure Kim will regale us with her encyclopedic knowledge of SQL Server over the coming months.  If you have SQL problems, particularly in the areas of performance and tuning, Kim is The Man, er, The Woman.

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook bluesky subscribe
About   Newsletter
Hosting By
Hosted on Linux using .NET in an Azure App Service

The beginning of a streaky LCD...and the end of Multiple Monitor Productivity?

June 09, 2004 Comment on this post [19] Posted in Musings
Sponsored By

I dig multi-monitors.  Everyone knows that you are MEASURABLY more productive when using more monitors.

Personally, I have three now. 

  • The one on the right is a 17" that I dug up from the garage.  It runs at 1152x864 at 32bit color.
  • The one on the left is my TabletPC running 1400x1060 at 16bit color.  I'm using MaxiVista to utilize that Tablet in its dock as a virtual monitor (This isn't a KVM or Remote Control thing, it literally shows up as a virtual display adapter).
  • Now, the MIDDLE one is a ViewSonic VA800 17" running at 1280x1024 at 32bit color.  It was about $700 a few years back, and I suspect its dying.

...which is interesting, as while I know that all hardware dies eventually, are LCDs supposed to die so quickly?

This particular monitor is showing weird color behaviors and bleeding.  Like if I have a large BLUE window, it seems to bleed off to the right and cause horizontal ghosts across the screen.

That said, what's the best price performance for LCDs these days?  Do they make 120-130dpi (this one is 96dpi) desktop monitors?  Clear and bright baby, clear and bright.

And actually, here's a question...is there anyone out there who DOESN'T think that LCDs are the "way and the light" and still have 21" CRTs heating their downstairs?

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook bluesky subscribe
About   Newsletter
Hosting By
Hosted on Linux using .NET in an Azure App Service

More thoughts around Code Generation and Extensible Programming Systems

June 08, 2004 Comment on this post [4] Posted in XML | Bugs | Tools
Sponsored By

Got forwarded an interesting link from Dr. Gregory Wilson at the University of Toronto.  He's an editor at Dr. Dobbs as well, and has an article up called Extensible Programming for the 21st Century that touches on many of the topics I've been concerned with lately. 

He makes some interesting assertions, starting with:

This article argues that next-generation programming systems will accomplish this by combining three specific technologies:

  • compilers, linkers, debuggers, and other tools will be plugin frameworks, rather than monolithic applications;
  • programmers will be able to extend the syntax of programming languages; and
  • programs will be stored as XML documents, so that programmers can represent and process data and meta-data uniformly.

Ok, #1 no problem, we arguable have this with the CLR's model, while #2 is a little different.  I assume he means actual keyword syntax, as opposed to the way we "changed" programming languages in C with preprocessors and inline functions.  Of course, LISP and Schema use macros to the point where one can't tell where the language starts or ends.  Additionally, custom language constructs like the "using" statement in C# that "expands" into a try/finally use of the IDisposable pattern extend the language within a specific context - so I'll buy #2 also.  I used to think that Number 3 is more of a stretch, but then you've got XAML sneaking up on us as well (not to mention our own foray into XML and CodeGen).

I supposed the question I am left with after reading his article is - hasn't all this already happened?

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook bluesky subscribe
About   Newsletter
Hosting By
Hosted on Linux using .NET in an Azure App Service

Phishing...for EVIL!

June 08, 2004 Comment on this post [2] Posted in Musings
Sponsored By

Just happened to get this in my evening email.  It's obviously a standard Phishing scam.  If you look at the source (as I usually do to most suspicious looking HTML email) you can see that the links point to urls like: http://www.scgi3-ebay-saw-cgi-ebayisapi-dll-registerenterinfo.xx.com.  Note that the first part of the URL is really just a very long subdomain, pointing to the evil person's actual domain (xx.com in this example.)  Additionally that domain's WHOIS record points to a fake person, blah blah.  Their domain points to freeservers.com which does roaming DNS which points to a webserver on their personal computer lord knows where.  When you submit your Credit Card it goes via an unencrypted Form POST right to their computer.  Evil!

What I found particularly interesting was what is revealed in this screen shot from my Outlook.  What's interesting...do you see it? 

The scrollbar is on the LEFT.  Further digging shows that the HTML body for this message was created with FrontPage and they've explicitly set the encoding to Windows-1252 and attempted to switch all the tags to dir="ltr."  However, Microsoft FrontPage when running in Right-To-Left Locales (Arabic, Hebrew, etc.) will default the HTML root tag as <html dir="rtl">.  As every OTHER tag in the document is explicitly marked dir="ltr" the document elements look OK, but since they missed the root tag, Outlook moves the scrollbar to the left, thus making their chicanery even more obvious.  Additionally it makes me wonder what country these folks are phishing from.

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook bluesky subscribe
About   Newsletter
Hosting By
Hosted on Linux using .NET in an Azure App Service

Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.