Friday, July 31, 2009

C#, Microsoft.Office.Core

In order to include Microsoft.Office.Core into your C# project, you need to add a reference to

Office

It's not Microsoft.Office, just Office, which is confusing.

Creating an Image from a User Control - C#

I wanted to create an image from a "screen shot" of my user control. I wanted to be able to save a picture of the user control at any given moment, save that image to a file for later use.

It's easy!

1. Create a Bitmap object of the size of your control.
Bitmap picture = new Bitmap (800, 600);

2. Create a Graphics object from the Bitmap Object.
Graphics graphx = Graphics.FromImage(picture);

3. Draw your user control to the Graphics object.
CreateControl(graphx);

4. Save your picture to a file.
picture.Save(@"C:\ControlImage.bmp");

It's that simple!

Monday, July 20, 2009

Graphics, Form Double Buffer

I was trying to double buffer a graphic control I wrote. It would blink when redrawing. I was not double buffering because, the graph would not draw. I was using the control's CreateGraphics() method. You should use the e.CreateGraphics() on the paint event. This allows you to use the double buffer without the error of not drawing.

Monday, July 13, 2009

Checked C# overflow

Using the 'checked' keyword in C#, you can check for an overflow.

checked
{
int a = someInt * someOtherInt;
}

This will check if there is an overflow.

You cannot check a nested method or something similar.

checked
{
int a = SomeMethod(b, c);
}

This will not check for overflow.

If you nest the check block in a try-catch. You can catch the overflow error.

try
{
checked
{
int a = someInt * someOtherInt;
}
}
catch (OverflowException)
{
// do something.
}

This is how to check for an overflow.

Friday, July 10, 2009

Drawing C#

C# drawing methods only take 16 bit points.

This is very limiting when it comes to drawing large scale graphics.

C# Nullable Structs

You can make a struct type nullable by adding a ? at the end of the type.

(e.g
Point? myNullablePoint = ...
int? myNullableInt = ..
)

and so on..

Thursday, July 9, 2009

Point C#

Points are structs and not nullable.

Just thought you should know.