tailieunhanh - Beginning Visual Basic 2005 phần 7

Hãy thử It Out Thêm một Dự án Thư viện Class một giải pháp hiện tại 1. Chuyển dự án InternetFavorites. 2. Lưu dự án và sau đó đóng Visual Studio 2005. 3. Mở các dự án Viewer Favorites. 4. Nhấp vào trình đơn File và chọn Add ➪ hiện dự án. 5. Điều hướng đến nơi bạn đã lưu InternetFavorites giải pháp của bạn và sau đó chọn Internet | Chapter 14 Figure 14-10 With the Backgroundimage property set when the base class needs to draw the background it will automatically draw the image. You should stop it from doing this otherwise you ll effectively be drawing the image twice in other words it ll draw the image and then you ll draw your own image on top of it. However you do need to mimic the functionality that erases the background otherwise things will not work properly. To do this you create a new SolidBrush that uses the current background color BackColor and paint it on the area that s marked as invalid ClipRectangle Protected Overrides Sub OnPaintBackground _ ByVal e As Paint the invalid region with the background brush Dim backgroundBrush As New SolidBrush BackColor backgroundBrush After you have painted the background you then need to draw the image. You can do this easily by using the Drawimage method of the Graphics object. But to stretch the image you need to provide a rectangle that describes the bounds of the image. Once you have that you give Drawimage both the image and the rectangle and the image is drawn. Paint the image If Not Backgroundimage Is Nothing Then Find our client rectangle Dim clientRectangle As New Rectangle 0 0 Width Height Draw the image Backgroundimage clientRectangle End If End Sub 470 Programming Custom Graphics Preserving the Aspect Ratio The problem you have now is that the image is stretched out of shape. Ideally you want to make the image bigger or smaller while preserving the aspect ratio which is the ratio between the width and the height of the image. The aspect ratio describes the ratio between the width and height of the image. The .NET Framework does not have any support for preserving the aspect ratio when it stretches an image. However with a little work you can do this yourself. Try It Out Preserving the Aspect Ratio 1. Open the Code Editor for PaintCanvas .

TỪ KHÓA LIÊN QUAN