October 2004 - Posts

Using LinearGradientBrush in C#

Well during my CSS change over I needed some background images for my headers. I wanted some simple gradient looks, but I only have 2 picture programs installed mspaint and Paint.Net, neither one of those does gradients (at least that I could find). I could download and install some picture program that has gradients but that is too much work and not much fun :)

So what do I do? I remember that there is some sort of Gradient stuff in GDI+ that I have never used. Then I do a simple google search and I turn up a nice walk through on how to use LinearGradientBrush on Bob Powell's GDI+ FAQ page. Then I open Visual Studio.Net and coded a little routine to generate a gradient image for my header. Here is the code for reference:

  1 using System.Drawing;
  2 using System.Drawing.Imaging;
  3 using System.Drawing.Drawing2D;
  5 ...
  7 public static void OutputGradientImage()
  8 {
  9 	using (Bitmap bitmap = new Bitmap(25, 75))
 10 	using (Graphics graphics = Graphics.FromImage(bitmap))
 11 	using (LinearGradientBrush brush = new LinearGradientBrush(
 12 		new Rectangle(0, 0, 25, 75),
 13 		Color.OliveDrab,
 14 		Color.LightGreen,
 15 		LinearGradientMode.Vertical))
 16 	{
 17 		brush.SetSigmaBellShape(0.25f, 0.75f);
 18 		graphics.FillRectangle(brush, new Rectangle(0, 0, 25, 75));
 19 		bitmap.Save("gradient.jpg", ImageFormat.Jpeg);
 20 	}
 21 }

It is amazing how easy this is to in .Net. Here is the image that the above code generated: 

I used that image as the background image for my top header. Then I changed the size to (25, 25) and generated the image for my sidebar / post headings.

At any rate I accomplished what I wanted to do, even it I had to code it myself. How big of a geek am I? ;)

Posted by puzzlehacker | with no comments

I updated my Blog CSS

I ran across JS Greenwood's blog the other day and I liked his layout so I stole some some of his CSS (with his permission of course). Thanks for the layout James!

If anyone has any design/color suggestions please feel free to leave a comment. God knows I can use all the artistic help I can get ;)

C# Tips and Tricks

I have been starting to participate more in newsgroups, in particular microsoft.public.dotnet.languages.csharp. I figured I have learned a lot from the .Net community and I should share my knowledge. It truly is amazing how much better you understand things when you have to explain it to others.

Anyway I ran across a very nice collection of C# and .Net articles/links on Jon Skeet's website (It doesn't look like he has a blog. Jon do you have a blog?). Jon is a C# MVP and is very active in the newsgroups and he is very knowledgeable on the subject.  If you are a beginner this information will really help you get an understanding of C# and .Net, in particular the newsgroup FAQ (even I picked up a few more things that I didn't know about).

Project Distributor

I just signed up on Project Distributor, a web application that Darren Neimke created to help distribute small pieces of software to the rest of the world. It has a feed so you can stay up to date.

I found PostXING by Chris Frazier, a simple but complete .Text, I mean Community Server :: Blogs, posting tool (which I'm currently using to post this message).

I have just uploaded my Run++ program up on Project Distributor.

If you haven't checked out Project Distributor yet you should, I think you can find some nice little utilities/tools there.

Posted by puzzlehacker | with no comments

Virtual PC 2004 SP 1

Get it while its hot: SP1 for Microsoft VPC is available at Microsoft Downloads.

The readme link on that page is currently broken, so here is what is new:

  • Many bug fixes
  • Command-line installation changes
  • Virtual Disk Precompactor
  • Updated Network Adapter Driver for Windows NT Workstation 4.0
  • Software Update for Shared Folders Under MS-DOS 6.22
  • Updated Virtual Machine Additions
  • Update to support TCP segmentation offloading
  • Update to support Group Policy Software Installation
  • Default performance option now set to "Run Virtual PC at maximum speed"
  • Performance improvements for XPSP2
  • Update to allow the name of the host computer and the virtual machine name to be read from within the guest operating system
  • The hotfix described in article 833506, which describes how Virtual PC 2004 may stop responding when a virtual machine is started

Now some of these things may be of particular interest to you, but what I'm happiest about is the Virtual Disk Precompactor.

From the readme file:

SP1 includes Virtual Disk Precompactor, a utility that is designed to "zero out"—that is, overwrite with zeros—any available blank space on a virtual hard disk.

We recommend that you use Virtual Disk Precompactor before you compact a dynamically expanding virtual hard disk. Using Virtual Disk Precompactor should result in a smaller compacted virtual hard disk. After you install SP1, Virtual Disk Precompactor.iso is located in the folder: \Program Files\Microsoft Virtual PC\Virtual Machine Additions\.

To use Virtual Disk Precompactor, you must first capture Virtual Disk Precompactor.iso. To do so, use the standard procedure for capturing .iso files. For more information about this procedure, see "To capture or release a CD or DVD" in the Virtual PC Help.

After you capture Virtual Disk Precompactor.iso, you can run Virtual Disk Precompactor by going to the CD drive of the virtual machine. To view Help for Virtual Disk Precompactor, type precompact -help. To run Virtual Disk Precompactor, type precompact.


[Geek Noise]

Lets hope that the XPSP2 issue is fixed.

Why can't you create a Generic Class that inherits from Attribute in C#?

Today I was creating a set of attributes for a project I'm working on and I attempted to create a generic class that inherits from Attribute and I didn't think anything more about it until I compiled and got the error: "error CS0698: A generic type cannot derive from 'Attribute' because it is an attribute class".

I thought this was kind of strange, so just out of curiosity I opened up the C# 2.0 Specification and sure enough in section 20.5.9 it forbids this very thing.

20.5.9 Attributes
An open type may not be used anywhere inside an attribute. A closed constructed type can be 
used as the argument to an attribute, but cannot be used as the attribute-name, because System.Attribute
cannot be the base type of a generic class declaration.
class A: Attribute 
public A(Type t) {...}
class B<T>: Attribute {} // Error, cannot use Attribute as base
class List<T>
[A(typeof(T))] T t; // Error, open type in attribute
class X
[A(typeof(List<int>))] int x; // Ok, closed constructed type
[B<int>] int y; // Error, invalid attribute name

Now I know Attribute is special but I'm puzzled as to why it can't handle open types. There must be some (and I'm sure there are many) implementation details that I don't understand.

Can anyone explain to me why a generic class declaration may not use Attribute as a direct or indirect base class? (Besides the obvious, because they say so in the specification :)

More Posts