Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
(ebook) Visual Studio .NET Mastering Visual Basic.pdf
Скачиваний:
137
Добавлен:
17.08.2013
Размер:
15.38 Mб
Скачать

SUMMARY 697

PictureBox1.Refresh() Me.Text = Int( _

100 * i / (PictureBox1.Image.Height - 2)).ToString & “%”

End If Next

End With PictureBox1.Refresh()

Me.Text = “Done diffusing image” End Sub

Open the ImageProcessing application and experiment with the algorithms described in this chapter. Change the parameters of the various algorithms and see how they affect the processed image. You can easily implement new algorithms by inserting the appropriate code in the inner loop’s body. The rest of the code remains the same. Some simple ideas include clipping one or more colors (force the red color component of each pixel to remain within a range of values), substituting one component for another (replace the red component of each pixel with the green or blue component of the same pixel), inverting the colors of the image (subtract all three color components of each pixel from 255), and so on. With a little imagination, you can create interesting effects for your images.

Summary

It’s been a long chapter, but graphics have never been simple. This may explain why they’re not among the most favorite programming topics, but they sure are fun. GDI+ brings VB graphics to a new level. GDI+ exposes a whole lot of functionality, and you have seen most of it in this chapter.

The basic object you’ll be using in your code to generate graphics is the Graphics object, and you can retrieve the Graphics object of a form or control with the CreateGraphics method. Then you can call this object’s drawing methods to generate graphics. If you’re going to display a few graphics elements on your form, you can put the corresponding statements in a subroutine that overwrites the OnPaint method, so that the form is redrawn every time it’s refreshed.

If you want to create graphics in response to user actions, keep in mind that anything you draw on the Graphics object returned by the CreateGraphics method is not permanent; it will be ignored when the form is refreshed. To generate permanent graphics on a form or PictureBox control, you must create a Graphics object based on the bitmap of the control. The bitmap is permanent, and it’s refreshed properly when the form is resized or temporarily covered by another form. As far as the drawing methods go, they’re the same no matter how you created the Graphics object.

Copyright ©2002 SYBEX, Inc., Alameda, CA

www.sybex.com

Copyright ©2002 SYBEX, Inc., Alameda, CA

www.sybex.com

Chapter 15

Printing with VB.NET

The topic of printing with Visual Basic hasn’t received much publicity in previous versions of the language, mostly because it’s a non-trivial topic and many developers used third-party tools to add print capabilities to their applications. VB.NET has simplified printing a little, but in some ways printing with VB.NET is more complicated than it used to be. The .NET Framework introduced mechanisms for generating printouts, but printing from within a VB application isn’t trivial. As you already know, there’s no control with built-in printing capabilities. It would be nice if certain controls, like the TextBox or the TreeView control, would print their contents, but this is not the case. The VB6 version of the RichTextBox control provided a printing method (the SelPrint method), which is no longer supported by the new version of the control that comes with VB.NET. If you want to print a few text paragraphs entered by the user on a TextBox control, you must provide your own code for that.

Printing with VB isn’t really complicated, but it requires a lot of code—most of it calling graphics methods. You must carefully calculate the coordinates of each graphic element placed on the paper, take into consideration the settings of the printer and the current page, and start a new page when the current one is filled. It’s like generating graphics for the monitor, so you need a basic understanding of the graphics methods, even if you’re only going to develop business applications. If you need to generate elaborate printouts, I would suggest that you look into thirdparty controls with built-in printing capabilities. In this chapter, you will find all the information you need, but before you decide to generate your own reports, weigh the complexity of your code versus a specialized tool. On the other hand, if you can group your project’s printouts into a few different types with common characteristics, you can then develop a class for each type of report and reuse these classes to produce all the printouts.

The first step in adding printing capabilities to an application is to decide what it is that you want to print and then design an application that allows users to specify what they want to print. The simplest printing job is to generate a printout from the contents of a TextBox control. Even this seemingly trivial task requires that you break the text lines, make sure that they’re confined within the page’s margins, detect when you have reached the end of the page, and start a new page as needed. You can’t even take for granted things like printing page numbers at the bottom of the page or the document’s title at the top of the page.

Copyright ©2002 SYBEX, Inc., Alameda, CA

www.sybex.com