ASP. NET interactive bitmap Form Design (3)

Source: Internet
Author: User
Tags finally block

Constructor
The constructor transmits three parameters: the point that contains the center coordinate of the circle, the radius of the circle, and a System. Drawing. Color structure (including the Color used to draw the circle contour ).

Then we calculate the border based on the center and radius, and set the pen color item as the color object we passed.

Drawing code
The Draw method overload is actually very simple: it creates a pen object based on the color object we saved in the constructor, and then uses this pen to call Graphics. the DrawEllipse method draws circles and transmits the border we created earlier.

Graphics, pens, and brushes
Here we need to explain Graphics, Pen, and Brush objects. (When starting to fill in our printable objects, we will see the Brush object .)

A Graphics object represents a virtualized drawing space associated with a real drawing space. Virtualization means that by drawing on a Graphics object, we can use the same Graphics method to draw on any type of actual surface associated with the object. For those who are used to using MFC or Microsoft Windows? For SDK programming users, the Graphics object is equivalent to the. NET version called "device context" (or DC) in Windows.

In this Windows Forms Application, the Graphics object passed to the Draw method will be associated with a window on the screen-here is PictureBox. When this code is used in our Microsoft ASP. NET application, the Graphics object passed to the Draw method will be associated with a bitmap image. It can also be associated with a printer or other devices.

The advantage of this solution is that we can plot on different surfaces using the same drawing code. In our drawing code, we don't need to know anything about the differences between screens, bitmaps, printers, etc. the. NET Framework (and the underlying operating system) can process all the details for us.

Using the same markup, pen and paint brush are virtualized drawing tools. The pen represents the line attributes-color, width, style, and even line bitmap. A paint brush represents the properties of a Filled Area-color, style, or even bitmap used to fill the area.

Clear (or at least Dispose) after Using)
Graphics, Pen, and Brush objects are associated with similar types of Windows objects. These Windows objects are allocated in the operating system memory-these memories are not managed by. NET runtime. Having these objects in the memory for a long time can cause performance problems. In Microsoft Windows 98, drawing problems may occur when the graph heap is full. Therefore, we should release these Windows objects as soon as possible.

When the corresponding. NET Framework object is completed and the memory is recycled, the Windows Object is automatically released when. NET is running. But it takes a long time to recycle the memory-if we don't release these objects quickly, all unfortunate things (including filling up Windows heap) can happen. In the ASP. NET version of the application, because many users access the application on the same server, this phenomenon is more serious.

Because these objects are associated with unmanaged resources, they implement the IDisposable interface. This interface has a method called Dispose, which separates. NET Framework objects from Windows objects and releases Windows objects, so that the computer is in a good state.

In this case, you only need to complete one task: Make sure that the Dispose method is called after the object is used.

The classic format of this content is displayed in Visual Basic. NET code: first create an object, use the object in a Try block, and Finally clear the object in the Finally block. Try/Finally can ensure that objects are cleared even if an exception occurs. (In this example, the plot method we call may not cause exceptions, so Try/Finally blocks are not required. But Mastering these skills is useful, so Dr. GUI also wants to show you this correct method .)

This form is common, So C # provides a private statement: using. C # The using statement in the Code is equivalent to the Declaration in Visual Basic. NET code and Try/Finally -- but it is more concise and convenient, and reduces the possibility of errors. (Dr. GUI does not know why Visual Basic. NET does not contain some using statements .)

Interface
Some (but not all) Plotting objects can be filled. Some objects (such as vertices and edges) cannot be filled because they are not closed areas, while objects such as Rectangles and circles can be hollow or filled.

On the other hand, just like processing all the callable objects as polymorphism, we can also process all the callable objects as polymorphism, which is very convenient. For example, just as we put all the objects that can be drawn into one set, and Draw objects by traversing the set and calling Draw on each object, we can place all the filled objects in a collection, regardless of the actual types of the filled objects. Therefore, we use a mechanism (such as inheritance) to obtain true polymorphism.
Because not all plotting objects can be filled, the declaration of the Fill method cannot be placed in the abstract base class .. . NET Framework does not allow multiple inheritance of classes, so it cannot be placed in another abstract base class. In addition, if we generate a writable object from never other base classes of the non-printable class, we cannot treat all the printable objects as polymorphism.

However,. NET Framework supports interfaces and provides a class that can implement any number of interfaces. The interface does not have any implementation-no code or data. Therefore, the class implementing the interface must provide all content.

All interfaces can contain only declarations. The following is the IFillable interface in C. Click here to view all source files in the new window.



C #
Public interface IFillable {
Void Fill (Graphics g );
Color FillBrushColor {get; set ;}
}


The following is equivalent Visual Basic. NET code. Click here to view all source files in the new window.

Visual Basic. NET

Public Interface IFillable
Sub Fill (ByVal g As Graphics)
Property FillBrushColor () As Color
End Interface

We do not need to declare methods, virtual/Overridable, abstract/MustOverride attributes, and any other items, because all methods and attributes in the interface are automatically set to public and abstract/MustOverride.

Use a property: the interface cannot contain data
Please note that although we cannot declare fields in the interface, we can declare an attribute because the attribute is actually implemented as a method.

However, this will put a burden on the Interface implementer, as shown in the following figure. The implementer must implement the get and set methods and any data required to implement this attribute. If the implementation is very complex, you can write a helper class to encapsulate some parts. Later in this article, we will show you how to use the helper class in a slightly different context environment.

Implementation Interface
We have already defined an interface. Now we can implement it in the class. Please note that we must provide the complete implementation of the implemented interface: you cannot select only one part of it.

Let's take a look at the code of the DFilledRectangle class in C.


C #
Public class DFilledCircle: DHollowCircle, IFillable
{
Public DFilledCircle (Point center, int radius, Color penColor,
Color brushColor): base (center, radius, penColor ){
This. brushColor = brushColor;
}

Public void Fill (Graphics g ){
Using (Brush B = new SolidBrush (brushColor )){
G. FillEllipse (B, bounding );
}
}
Protected Color brushColor;
Public Color FillBrushColor {
Get {
Return brushColor;
}
Set {
BrushColor = value;
}
}
Public override void Draw (Graphics g ){
Fill (g );
Base. Draw (g );
}
}

The following is the code of the DFilledRectangle class in Visual Basic. NET.


Visual Basic. NET
Public Class DFilledRectangle
Inherits DHollowRectangle
Implements IFillable
Public Sub New (ByVal rect As Rectangle ,_
ByVal penColor As Color, ByVal brushColor As Color)
MyBase. New (rect, penColor)
Me. brushColor = brushColor
End Sub
Public Sub Fill (ByVal g As Graphics) Implements IFillable. Fill
Dim B = New SolidBrush (FillBrushColor)
Try
G. FillRectangle (B, bounding)
Finally
B. dispose ()
End Try
End Sub
Protected brushColor As Color
Public Property FillBrushColor () As Color _
Implements IFillable. FillBrushColor
Get
Return brushColor
End Get
Set (ByVal Value As Color)
BrushColor = Value
End Set
End P

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.