- Table of Contents
- .NET Book Recommendations
- What Is .NET?
- The Microsoft .NET Framework
- The Common Language Runtime (CLR), the Common Type System (CTS), and the Common Language Specification (CLS)
- .NET Framework Class Library
- Visual Studio .NET
- .NET Enterprise Servers and .NET My Services
- .NET Compliant Languages
- C#
- Visual Basic .NET (VB .NET)
- ASP.NET
- XML Web Services
- ADO.NET
- XML.NET
- Windows Forms
- Why .NET?
- Displaying Errors with the Error Provider
- COM Interoperability
- Comparing Java and .NET
- Calling Unmanaged Code
- .NET Application Security
- Code Access Security
- .NET Standards Support
- Numeric Types in the .NET Framework
- Working with Strings
- Formatting Strings
- Trimming Character Strings
- Comparing Strings in .NET 2.0
- Arrays and Collections
- Arrays as Class Members
- Sorting a Multi-Dimensional Array
- File I/O (System.IO)
- Working with File Names
- Using the File System
- Working with Files and Directories
- Monitoring the File System
- Working with Streams
- Working with Text Encodings
- Working with Date and Time
- Extending the DateTime Class
- Fun with Dates
- Exceptions
- Delegates
- Events
- Asynchronous Programming
- Asynchronous File I/O
- Timers
- Random Numbers
- Serialization
- MultiThreading (System.Threading)
- Multi-Threading Overview
- The Managed Thread Pool
- Managed Threading
- Thread Synchronization
- Synchronizing Data Access
- Trace Debugging
- Tracing in .NET 2.0
- ASP.NET Trace
- Validating User Input in ASP.NET Web Pages
- Event Logging
- Monitoring Application Performance
- Accessing the Registry
- Accessing Environment Information
- Environment Variables in .NET 2.0
- Managing Windows Forms Applications
- Working with Email
- Working with Graphics
- Animating a Background
- Working with Images
- Loading and Saving Bitmap Images
- Displaying Images
- Official Documentation
- Drawing Cycloid Curves
- Simulating the Spirograph
- Building International Web Applications
- .NET Compact Framework
- Mobile Web Development with ASP.NET
- Speech Technologies
- Microsoft MapPoint Web Service
- Working with Typed DataSets
- Using Relationships in DataSets
- DataColumn Expressions
- Playing Simple Sounds
- Playing Sounds with .NET 2.0
- Returning an Image in a Web Page
- RSS
- Best Practices Project Structure
- Best Practices Application Blocks
- The Data Access Application Block
- The Exception Management Application Block
- Best Practices — Performance
- Best Practices — Performance and Scalability
- Best Practices - Testing
- Reading the Tea Leaves, 2005
- Predictions: A Look Back at 2005, and a Look Ahead to 2006
- .NET Downloads
- Application Deployment Overview
- Application Deployment — Versioning
- Application Deployment — Version Policy
- Application Deployment — Packaging and Distribution
- .NET Remoting Overview
- A Remoting Demonstration
- Remoting Configuration
- Remoting: Lifetimes and Leases
- Remoting: Other Issues
- Attributes
- Writing Custom Attributes
- Accessing Attributes in Code
- Reflection
- Class Design: Inheritance, Interface, or Composition?
- The TriTryst Game
- Console Applications in .NET 2.0
- New File I/O Methods in .NET 2.0
- Building Projects with MSBuild
- Unmanaged Callbacks in .NET 2.0
- Timer Troubles
- Non-Rectangular Windows Forms
- Windows Forms Transparency
- 10 Things I Hate About Visual Basic
- 10 Things I Hate About C#
- Background Processing with Idle Time
- Scaling Windows Forms
- Reading and Writing Binary Data
- New Memory Management Functions in .NET 2.0
- Compatibility Between .NET 1.1 and .NET 2.0
- Managed Debugging Assistants in .NET 2.0
- XDir: A Program for Viewing Directory Sizes
- The Microsoft.VisualBasic Namespace
- Operator Overloading
- Working with GPS Data
- Hidden Visual Studio Tools
- .NET 3.0
- The .NET 2.0 Stopwatch Class
- Nullable Types
- Drawing Rotated Text
- Unsafe Code
- Other .NET Languages
- Compiler Directives
- Safe Handles
- Predictions, 2007 Edition
- New Features in C# 3.0
- Generics
- Network Client Programming
- On the Misuse of Exceptions
- Maximum Object Size in .NET
- More on Maximum Object Sizes
- Keyed Collection Memory Limitations
- Matching String Endings
- Allocating Small Data Structures
- Grumbling About Limitations
- Some Thoughts on the Nature of What We Do
- Working with Predicates in Collections
- Working with DataReaders
- Outputting XML with XmlWriter
- Writing XML Data
- Working with Compression
- Another Look at Compressed Streams
- Compressing a Very Large File
- Canonical URIs
- Constructing URIs
- Using OneWayAttribute for Remote Calls
- Selecting a Garbage Collector
- Linked List
- Linked List Application - The MRU List
- Auto-implemented Properties in C#
- The HashSet Collection
- Looking Ahead: 2018
- An Experiment in Optimization
- A Larger Integer
- Extension Methods
- Language Integrated Query (LINQ)
- Variable Length Parameter Lists
- The ReaderWriterLockSlim Synchronization Primitive
- Sorting a Text File
- Sorting a Large Text File
- Using ListView with Large Data Sets
- LINQ One-Liners
- Regular Expression Optimization
- Random File I/O
- Computing the Size of a Structure
- More on Computing Structure Sizes
- UnmanagedMemoryStream
- Dynamically Loading Code
- Building a String Table
- Delegates Versus Function Pointers
- Visual Studio Editor Features
- A Simple Profile Timer
- New Features in C# 4.0
- IEnumerator or IList?
- New Features in .NET 4.0
- Set Operations with IEnumerable and HashSet
- Using File Locks
- Extending Object Functionality
- Clearing a HashSet
- When Hash Codes Matter
- Parsing Command Line Options
- Creating a Single-Instance Program
- Asynchronous Windows Forms Events
- The BackgroundWorker Component
- Fixing a Dumb Mistake
- Thinking About Multi-Threaded Programs
- JavaScript Object Notation
- Useful .NET-related Sites
- Markov Models
- Building an Order 0 Markov Model
- Higher Order Markov Models
- Webmaster's Guide to robots.txt
- An Overview of the Parallel Extensions to .NET
- Parallel Extensions Synchronization Objects
- Thread Safe Collections
- A Bug and a Conundrum
- Another Bug and an Answer
- Task Parallel Library
- Good and Bad Ideas in C#
- Parallel LINQ
- Copying Large Files
- Replacing File.Copy
- Learning from Our Mistakes
- Symbolic Links
- There Is No Easy Fix
- Tracking Hurricanes
- Examining Hurricane Data
- Searching for Multiple Strings
- Simple JSON Processing
- Aho-Corasick String Searching New
- Writing a Web Crawler New
- Web Crawler Politeness New
- Source Control Management New
- Informit Reference Library
Displaying Images
Last updated Sep 9, 2004.
You display images by calling the DrawImage method of the Graphics object that defines the surface on which you want the image to appear. For example, if you have a form with a panel, panel1, the simplest way to display the image is like this:
[C#]
using (Graphics g = panel1.CreateGraphics())
{
g.DrawImage(bmp, 0, 0);
}
[Visual Basic]
Dim g as Graphics = Panel1.CreateGraphics()
Try
g.DrawImage(bmp, 0, 0)
Finally
g.Dispose()
End Try
That will draw the bitmap at position (0, 0) (the upper left-hand corner) of the panel. This is the easiest, but not necessarily the most efficient way to draw an image. When you specify the position but not the size, GDI performs automatic scaling of the image. GDI scales the image using the resolution ("pixels per inch", defined in the image as HorizontalResolution and VerticalResolution) values of the image and the display device. If the image's resolution doesn't match the display device's resolution (which is usually 96 pixels per inch), then GDI will perform automatic scaling.
You can avoid automatic scaling by specifying a rectangle in which GDI is to display the image. If you want to display the image in its full size, you simply write:
[C#]
g.DrawImage(bmp, new Rectangle(0, 0, bmp.Width, bmp.Height);
[Visual Basic]
g.DrawImage(bmp, new Rectangle(0, 0, bmp.Width, bmp.Height);
There are 30 overloads of Graphics.Draw that let you specify the location and size of the image in different ways. You can scale, crop, rotate, reflect, and skew images to create many different effects. See the Graphics.Draw documentation the .NET Framework SDK Reference for full details.
Creating Bitmaps
In the previous sections you've seen how to work with images that already exist. But suppose you want to create your own bitmap image, perhaps as part of a paint program. How would you do that?
The easy part is creating the bitmap. You can create a blank Bitmap object instance by calling the Bitmap constructor, passing it the width and height of the new image. You can also pass it a reference to a Graphics object so that the bitmap will be created with the same resolution.
After you've created the bitmap, you need to be able to manipulate it: draw lines and shapes, and write text to it. You do that by creating a Graphics object that uses the bitmap as its canvas. The Graphics.FromImage method will create such a graphics object. You can then call Graphics methods to create whatever pretty picture you like, and save the bitmap as shown in the previous section. The code below provides an example.
[C#]
using (Bitmap bmp = new Bitmap (100, 100))
{
using (Graphics g = Graphics.FromImage(bmp))
{
// fill it with white
g.FillRectangle(Brushes.White, 0, 0, bmp.Width, bmp.Height);
// and draw a black diagonal line
g.DrawLine(Pens.Black, 0, 0, bmp.Width-1, bmp.Height-1);
}
bmp.Save("myimage.bmp", System.Drawing.Imaging.ImageFormat.Bmp);
}
[Visual Basic]
Dim bmp As New Bitmap(100, 100)
Try
Dim g As Graphics = Graphics.FromImage(bmp)
Try
' fill it with white
g.FillRectangle(Brushes.White, 0, 0, bmp.Width, bmp.Height)
' and draw a black diagonal line
g.DrawLine(Pens.Black, bmp.Width - 1, 0, 0, bmp.Height - 1)
Finally
g.Dispose()
End Try
bmp.Save("myimage.bmp", System.Drawing.Imaging.ImageFormat.Bmp)
Finally
bmp.Dispose()
End Try


Account Sign In
View your cart