Creating a Bitmap Object
The Bitmap class provides about a dozen overloaded forms of the constructors. You can create a Bitmap object from a bitmap file, or from Image, Stream, string, or Type objects. When you create a Bitmap object, you can also specify the size of the bitmap, the resolution of the Graphics object, and the pixel format of the bitmap.
The code snippet in Listing 7.17 creates Bitmap objects from an Image and file name with or without the size of the Bitmap included.
Listing 7.17 Creating Bitmap objects from different sources
// Creating an Image object Image curImage = Image.FromFile("myfile.gif");
// Creating a Bitmap object from a file name Bitmap curBitmap1 = new Bitmap("myfile.gif");
// Creating a Bitmap object from an Image object Bitmap curBitmap2 = new Bitmap(curImage);
// Creating a Bitmap object with size and image Bitmap curBitmap3 = new Bitmap(curImage, new Size(200, 100) );
// Creating a Bitmap object with no images Bitmap curBitmap4 = new Bitmap(200, 100);
Besides the constructor, the Bitmap class provides two static methods—FromHicon and FromResource—which can be used to create a Bitmap object from a window handle to an icon and from a Windows resource (.res file), respectively.
7.5.2 Viewing a Bitmap
Viewing a bitmap using the Bitmap class is similar to viewing an image. After constructing a Bitmap object, you just pass it as a parameter to DrawImage. The following code snippet creates a Bitmap object from a file and views the bitmap by calling the DrawImage method of a Graphics object associated with a form. You can write this code on a menu or a button click event handler.
Graphics g = this.CreateGraphics(); Bitmap bitmap = new Bitmap("myfile.jpg"); g.DrawImage(bitmap, 20, 20); g.Dispose();
'C#.NET GDI' 카테고리의 다른 글
[MSDN]이미지 메타데이터 읽기 (0) | 2010.04.27 |
---|---|
[PRINT]인쇄 미리 보기로 폼 보기(Visual C#) (0) | 2010.01.03 |
[IMAGE & SAVE] 이미지 저장하기 SaveFile 이벤트~ (0) | 2010.01.03 |
[PRINT]프린트대화상자가 나타나지 않게 하려면 어떻게? (0) | 2010.01.03 |
더블 버퍼링 (0) | 2010.01.03 |
[IMAGE] 이미지 합치기 (0) | 2009.12.15 |
[PRINT] 프린트 출력에 관한 예제 (0) | 2009.12.15 |
[PRINT] 내컴에 설치된 프린트 보기 (0) | 2009.12.15 |
[BITMAP] 비트맵에다가 그림을 그려서 출력까지 (0) | 2009.12.15 |
[MSDN]인쇄 미리 보기로 폼 보기(Visual C#) (0) | 2009.12.15 |