Next time I work with windows metafile formats and they turn out black bitmaps…

Next time I work with windows metafile formats I should remember that whenever I save the metafile to a bitmap I should clear the black color.

I got a request from a client that i need to show and resize bitmap images (jpeg, gif, tiff, png and bmp) along with .wmf files in a PictureBox, resize the images and embed them into reports.

Bitmaps were not a problem, but resizing and saving metafiles would replace the white areas with black color…

This issue had been addressed in the following forums:
Forum at MSDN

Forum at VelocityReviews

My short term solution:

// load bitmap from file
// sFile is the file name
FileInfo instance = new FileInfo(sFile);
if ((instance.Extension == “.wmf”) || (instance.Extension == “.WMF”))
{

System.Drawing.Imaging.Metafile mf = (Metafile)Image.FromFile(sFile, true);

mf.Save(instance.Name+”_temp.bmp”, ImageFormat.Bmp);

Image i = Image.FromFile(instance.Name + “_temp.bmp”, true);

bmp = new Bitmap(i);
bmp.MakeTransparent(Color.Black);
}

The example above uses file streams but it works as well with memory streams :-p, yes, I’m not going around leaving a bunch of temporary files on the hard drive without doing clean up…

Yet another complaint on the .NET Framework versioning

It just doesn’t make sense that in order to add up functionality to the 2.0 framework without touching the runtime, yet another framework version is created and deployed in a different folder. It is extremely confusing as to what will be the implications. Let’s say I have an existing 2.0 application and I want to take advantage of the WinFX in order to add/implement workflows. Do I have first to start worrying about how to migrate my current application from 2.0 to 3.0 or the classes we currently use from the 2.0 framework are still valid under the 3.0, amen of the new WinFX additions. Why not just adding a new namespace and new assembly for this piece, if it is dependent on the 2.0 framework but won’t replace it????
Migrating from 1.0 to 1.1 caused some pain, more pain (necessary evil IMHO) to migrate from 1.1 to 2.0. Do we have another migration at sight, or it would be just an add up?
It would be worth to have an article published on MSDN about the cause of this decision.

WinFX 3.0 Renamed .NET Framework 3.0

….

I found the article on MSDN, it still won’t explain why the versioning convention. As some bloggers have already discussed, having a Framework 3.0 and CLR 2.0 doesn’t make much sense when they precisely state “…because .NET Framework 3.0 is an additive release”
Deploying Microsoft .NET Framework Version 3.0

I knew I had read this somewhere….why manhole covers are round?

A couple of days ago our team gathered for lunch and a quick meeting, we discussed job interview techniques and our personal experiences going to job interviews. One of the brain teaser questions that came up was the shape of the manhole covers. I couldn’t remeber where I had read this before. Enjoy, it is hilarious…

(Original post taken from here)

If Richard Feynman applied for a job at Microsoft

Interviewer: Now comes the part of the interview where we ask a question to test your creative thinking ability. Don’t think too hard about it, just apply everyday common sense, and describe your reasoning process.

Here’s the question: Why are manhole covers round?

Feynman: They’re not. Some manhole covers are square. It’s true that there are SOME round ones, but I’ve seen square ones, and rectangular ones.

Interviewer: But just considering the round ones, why are they round?

Feynman: If we are just considering the round ones, then they are round by definition. That statement is a tautology.

Interviewer: I mean, why are there round ones at all? Is there some particular value to having round ones?

Feynman: Yes. Round covers are used when the hole they are covering up is also round. It’s simplest to cover a round hole with a round cover.

Interviewer: Can you think of a property of round covers that gives them an advantage over square ones?

Feynman: We have to look at what is under the cover to answer that question. The hole below the cover is round because a cylinder is the strongest shape against the compression of the earth around it. Also, the term “manhole” implies a passage big enough for a man, and a human being climbing down a ladder is roughly circular in cross-section. So a cylindrical pipe is the natural shape for manholes. The covers are simply the shape needed to cover up a cylinder.

Interviewer: Do you believe there is a safety issue? I mean, couldn’t square covers fall into the hole and hurt someone?

Feynman: Not likely. Square covers are sometimes used on prefabricated vaults where the access passage is also square. The cover is larger than the passage, and sits on a ledge that supports it along the entire perimeter. The covers are usually made of solid metal and are very heavy. Let’s assume a two-foot square opening and a ledge width of 1-1/2 inches. In order to get it to fall in, you would have to lift one side of the cover, then rotate it 30 degrees so that the cover would clear the ledge, and then tilt the cover up nearly 45 degrees from horizontal before the center of gravity would shift enough for it to fall in. Yes, it’s possible, but very unlikely. The people authorized to open manhole covers could easily be trained to do it safely. Applying common engineering sense, the shape of a manhole cover is entirely determined by the shape of the opening it is intended to cover.

Interviewer (troubled): Excuse me a moment; I have to discuss something with my management team. (Leaves room.)

(Interviewer returns after 10 minutes)

Interviewer: We are going to recommend you for immediate hiring into the marketing department.

Forms authentication and client caching in ASP.NET 1.1

We got really sad news today, the type of news that makes you have sustained stomachache for a few days.
I’m not going to blog about the way my stomach feels but to remind me that I like what I do and this is not just a job, it’s a pleasure.

The Problem:

On one of our web projects that uses Forms authentication.
After the authentication process we create an encrypted ticket,
create the cookie that will be used by the FormsAuthentication
provider and redirect to the requested page:

Dim authTix As New
FormsAuthenticationTicket(1, UserName, DateTime.Now,
DateTime.Now.AddMinutes(60), isCookiePersistent, UserData)

Dim encryptedTix As String =
FormsAuthentication.Encrypt(authTix)

Dim authCookie As New
HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket)


authCookie.Expires = authTicket.Expiration

Context.Response.Cookies.Add(authCookie)
FormsAuthentication.RedirectFromLoginPage(UserName,
False)

During user log out we clear the session, call the
FormsAuthentication.SignOut() and redirect the user to the login page.

We had, however, an odd behavior. After the user has logged out of the
application, he could, by clicking the back button on the same browser
windows, navigate to the previous pages he opened. These pages were in
the secure area. These pages were not requested to the server, these
requests did not hit the server so I presumed the user was seing cached
pages in the browser.

The Solution:
To use the directive
Context.Response.Cache.SetNoStore()
on all the secure pages that shouldn’t be cached.
For more info on the framework class HttpCacheability go to MSDN

For more info on Cache-Control Headers on HTTP 1.1 go here

The Side Effect Problem:

It seems IE does not store the file in its temporary Internet files folder whenever the server specifies the “no-store” http cache directive ; as a consequence, it cannot feed Acrobat/Excel or whatever external application with the output of your page. if the application has excel or pdf reports on the fly, they will generate an error if the http directive is sent in the response.

http://support.microsoft.com/kb/243717/en-us

The Second Solution:
To remove this cache header whenever you need to send to the client a file to be opened in the browser.

SQL Server and binary fields (images)

No matter how much I disagree on putting image files into SQL Server, I had to handle this on a project.

We had to provide update statements to insert binary data into production servers from the data we had on the test servers. The statement had to be written in T-SQL, period.

Everything would have been a fairy tail if Query Analyzer wouldn’t have the 8K result limit. Most of the images were bigger than 8K. This limitation does not apply to the data you use on the query you’re running but on the buffer that stores the results to show in QA.

We ended up writing a small console app with ADO.NET to retrieve the binary data, but guess what, you cannot insert this data as it is with a T-SQL statement. We shortly after discovered we could have used a third party tool such as SQL Bundle from red-gate to do this. Did I say before that I just LOVE SQL Compare?
The binary data has to be converted to its hexadecimal equivalent so it can be used in an update or insert statement, you can not insert whatever you get from the database into your T-SQL queries.

Code samples follow:

The ADO.NET part (pretty basic):

this.sqlConnection1.Open();
this.sqlCommand1.CommandText=”SELECT [Image field] FROM table where ID=something”;

byte[] barrImg=(byte[])sqlCommand1.ExecuteScalar();

string strfn=Convert.ToString(“yourfilename.txt”);
FileStream fs=new FileStream(strfn,FileMode.CreateNew);
fs.Write(barrImg,0,barrImg.Length);
fs.Flush();
fs.Close();

The hexadecimal conversion part, basic too:

char[] hexDigits = {‘0’, ‘1’, ‘2’, ‘3’, ‘4’, ‘5’, ‘6’, ‘7’,’8′, ‘9’, ‘A’, ‘B’, ‘C’, ‘D’, ‘E’, ‘F’};
char[] chars = new char[barrImg.Length * 2];
for (int i = 0; i <>
{
int b = barrImg[i];
chars[i * 2] = hexDigits[b >> 4];
chars[i * 2 + 1] = hexDigits[b & 0xF];
}

string results;
results=new string(chars);

StreamWriter strWr = new StreamWriter(“yourhexdatafile.txt”);
strWr.Write(results);
strWr.Flush();
strWr.Close();

Note: VB.NET has a Hex function for the hexadecimal conversion.

Debugging Javascript in Visual Studio

I got this from a colleague:

  1. Open Microsoft Internet Explorer.
  2. On the Tools menu, click Internet Options.
  3. On the Advanced tab, locate the Browsing section, and uncheck the Disable script debugging check box, and then click OK.
  4. Close Internet Explorer.
  5. In your JavasSript function add the keyword debugger . This causes VS.NET to switch to debug mode when it runs that line.
EX : –
function OnLookup()
{
debugger;
var xr = new XMLHttpRequest();

6. Run your ASP.Net application in debug mode.

Regular expressions for validating currency

Embedded as I am in globalizing applications I thought I would create this entry for future reference.

More than once I have to validate user input and more than once that input is a currency value. The currency symbols are not part of the expressions here.

For en-US or en-CA:

^(\d{1,3})(,\d{3})*(\.\d{2})?$

fr-CA currency formatting regular expression:

^(\d{1,3})(\s{1}\d{3})*(,\d{2})?$

Ttyl!

The mysterious get_aspx_ver.aspx page in Visual Studio 2003

I am currently working on a VS 2003 solution using VB.NET. Each time one of the team members loaded the web project our exception handling block would log a weird exception: the page requested get_aspx_ver.aspx. The exception tried to load our default error page and emailed us the proper notification.
Weird… we are not executing any code when loading the project… wrong, this mysterious page is called…

Visual Studio .NET 2003 makes an http request to the non existent web page: http://yourserver/yourweb/get_aspx_ver.aspx in order to determine the ASP.NET version installed on your web server (this information is returned in the http headers for the request). Since the page does not exist on your web server, your error handler is called.

One possible work around is to have your error handler ignore failures to this page, which is exactly what we did.

Related problems are described here:
http://support.microsoft.com/default.aspx?scid=kb;en-us;825792

and on this post in aspnetresources.com.

TTYL!

Localization Management Toolkit (beta)

A colleague and I wrote an article describing a toolkit we developed. This toolkit automates the management process of resources for localization with .NET 2.0. The article made it to the home page in DevX as featured article.
The permalink is here
http://www.devx.com/dotnet/Article/33422
We are both eager to know if someone is using a similar tool or plans to use this Toolkit.
Thanks!