亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

圖形和 Windows 窗體

系統(tǒng) 2432 0

圖形和 Windows 窗體

介紹 GDI+

公共語(yǔ)言運(yùn)行庫(kù)使用 Windows 圖形設(shè)備接口 (GDI) 的高級(jí)版本,其名稱(chēng)為 GDI+。GDI+ 旨在提供良好的性能和易用性。它支持二維圖形、版式和圖像。

新的二維功能包括下列內(nèi)容:

  • 對(duì)所有圖形基元的 Alpha 混合支持
  • 消除鋸齒
  • 漸變填充和紋理填充
  • 寬線(xiàn)
  • 基數(shù)樣條
  • 可縮放區(qū)域
  • 浮點(diǎn)坐標(biāo)
  • 復(fù)合線(xiàn)
  • 嵌入鋼筆
  • 高質(zhì)量的篩選和縮放
  • 大量的線(xiàn)型和筆尖選項(xiàng)

圖像支持包括下列內(nèi)容:

  • 對(duì)圖像文件格式(如 .jpeg、.png、.gif、.bmp、.tiff、.exif 和 .icon)的本機(jī)支持
  • 用于編碼和解碼任意光柵圖像格式的公共接口
  • 用于動(dòng)態(tài)添加新圖像文件格式的可擴(kuò)展結(jié)構(gòu)
  • 對(duì)通用點(diǎn)操作(如亮度、對(duì)比度、色彩平衡、模糊和溶解)的本機(jī)圖像處理支持。對(duì)通用轉(zhuǎn)換(如旋轉(zhuǎn)、裁切等)的支持。

顏色管理

對(duì) sRGB、ICM2 和 sRGB64 的支持

版式支持包括下列內(nèi)容:

  • 本機(jī) ClearType 支持
  • 紋理填充和漸變填充的文本
  • 在所有平臺(tái)上對(duì) Unicode 的完全支持
  • 對(duì)所有 Windows 2000 腳本的支持
  • Unicode 3.0 標(biāo)準(zhǔn)的更新
  • 文本行服務(wù)支持,可使文本更具可讀性

GDI+ 可與 Windows 窗體和 Web 窗體一起使用。例如,Web 窗體控件可使用基于用戶(hù)輸入的 GDI+ 動(dòng)態(tài)生成 .jpeg 文件,并從 Web 頁(yè)引用它。

本節(jié)集中討論 Windows 窗體。

GDI 和 GDI+ 之間的差異

本節(jié)的目的是幫助您了解使用 GDI+ 的基礎(chǔ)知識(shí)。開(kāi)始之前,有必要指出 GDI 和 GDI+ 之間的最大差異。GDI 具有有狀態(tài)的編程模型,而 GDI+ 具有無(wú)狀態(tài)的編程模型。使用 GDI,可在繪圖表面上設(shè)置屬性(如前景色和背景色),然后在它上面進(jìn)行繪制。例如,若要繪制黑色文本字符串,代碼有點(diǎn)類(lèi)似于下面的示例。

function doClick(index, numTabs, id) { document.all("tab" + id, index).className = "tab"; for (var i=1; i <style type="text/css"> td.code { padding:0,10,0,10; border-style:solid; border-width:1; border-bottom:0; border-top:0; border-right:0; border-color:cccccc; background-color:ffffee } td.tab { text-align:center; font:8pt verdana; width:15%; padding:3,3,3,3; border-style:solid; border-width:1; border-right:0; border-color:black; background-color:eeeeee; cursor:hand } td.backtab { text-align:center; font: 8pt verdana; width:15%; padding:3,3,3,3; border-style:solid; border-width:1; border-right:0; border-color:black; background-color:cccccc; cursor:hand } td.space { width:70%; font: 8pt verdana; padding:0,0,0,0; border-style:solid; border-bottom:0; border-right:0; border-width:1; border-color:cccccc; border-left-color:black; background-color:white } </style>

            Graphics g ;
g.ForeColor = Color.Black;
g.BackColor = Color.White;
g.Font = new Font("Times New Roman", 26);
g.DrawString("Hello, World", 0, 0);

          
            Dim g as Graphics
g.ForeColor = Color.Black
g.BackColor = Color.White
g.Font = new Font("Times New Roman", 26)
g.DrawString("Hello, World", 0, 0)

          
C# VB

使用 GDI+ 時(shí),始終將要使用的屬性作為繪圖命令的一部分傳遞。上面的代碼更改為如下所示。

            Graphics g ;
Color foreColor = Color.Black;
Color backColor = Color.White;
Font font = new Font("Times New Roman", 26);
g.FillRectangle(new SolidBrush(backColor), ClientRectangle);
g.DrawString("Hello World", font, new SolidBrush(foreColor), 15, 15);

          
            Dim g As Graphics;
Dim foreColor As Color = Color.Black;
Dim backColor As Color = Color.White;
Dim vbFont As New Font("Times New Roman", 26);
g.FillRectangle(New SolidBrush(backColor), ClientRectangle);
g.DrawString("Hello World", vbFont, New SolidBrush(foreColor), 15, 15);

          
C# VB
GDI+ 命名空間

GDI+ 類(lèi)駐留于 System.Drawing System.Drawing.Drawing2D System.Drawing.Imaging System.Drawing.Text 命名空間中。這些命名空間包含在程序集 System.Drawing.DLL 中。

創(chuàng)建圖形對(duì)象

GDI+ 繪圖表面由 Graphics 類(lèi)表示。為了使用 GDI+,首先需要一個(gè)對(duì)圖形對(duì)象的引用。通常,在控件或窗體的 Paint 事件中或者在

可以通過(guò)為 Paint 事件創(chuàng)建事件處理程序來(lái)處理該事件。

            public class GdiPlusDemo : Form {

   public GdiPlusDemo () {
        //Hook the paint event of the form.
        this.Paint += new PaintEventHandler(form1_Paint);
    }

    private void form1_Paint(object sender, PaintEventArgs pe) {
        Graphics g = pe.Graphics;

        //Simply fill a rectangle with red.
        g.FillRectangle(new SolidBrush(Color.Red), 40, 40, 140, 140);
    }
}

          
            Public Class GdiPlusDemo : Inherits Form

    Public Sub New()
        ' Hook the paint event of the form.
        AddHandler Me.Paint, AddressOf form1_Paint
    End Sub

    Private Sub form1_Paint(sender As Object, pe As PaintEventArgs)
        Dim g As Graphics = pe.Graphics

        ' Simply fill a rectangle with red.
        g.FillRectangle(New SolidBrush(Color.Red), 40, 40, 140, 140)
    End Sub
End Class

          
C# VB

更常用的是,可以創(chuàng)建 OnPaint 方法的子類(lèi)并重寫(xiě)該方法。

            public class GdiPlusDemo : Form {

   public GdiPlusDemo () {
   }

   protected override void OnPaint(PaintEventArgs pe) {
        Graphics g = pe.Graphics;
        g.FillRectangle(new SolidBrush(Color.Red), 40, 40, 140, 140);
    }
}

          
            Public Class GdiPlusDemo : Inherits Form

   Public Sub New()
   End Sub

   Protected Overrides Sub OnPaint(pe As PaintEventArgs)
        Dim g As Graphics = pe.Graphics
        g.FillRectangle(New SolidBrush(Color.Red), 40, 40, 140, 140)
   End Sub
End Class

          
C# VB

還可以從 Image 的任何派生類(lèi)創(chuàng)建 Graphics 對(duì)象實(shí)例。

            Bitmap newBitmap = new Bitmap(600,400,PixelFormat.Format32bppArgb);
Graphics g = Graphics.FromImage(newBitmap);
g.FillRectangle(new SolidBrush(Color.Red), 40, 40, 140, 140);
newBitmap.Save("c:\\temp\\TestImage.jpg", ImageFormat.Jpeg) ;

          
            Dim newBitmap As Bitmap = New Bitmap(600,400,PixelFormat.Format32bppArgb)
Dim g As Graphics = Graphics.FromImage(newBitmap)
g.FillRectangle(New SolidBrush(Color.Red), 40, 40, 140, 140)
newBitmap.Save("c:\temp\TestImage.jpg", ImageFormat.Jpeg)

          
C# VB

創(chuàng)建 Graphics 對(duì)象后,可以使用它繪制線(xiàn)、填充形狀、繪制文本等。與 Graphics 對(duì)象一起使用的主要對(duì)象如下所示。

Brush

用于使用圖案、顏色或位圖填充封閉表面。

Pen

用于繪制線(xiàn)和多邊形,包括矩形、弧線(xiàn)和扇形。

Font

用于描述用來(lái)呈現(xiàn)文本的字體。

Color

用于描述用來(lái)呈現(xiàn)特定對(duì)象的顏色。在 GDI+ 中,顏色可以是 Alpha 混合效果的。

Alpha 混合
顏色可以是 Alpha 混合的,這使得易于創(chuàng)建基于顏色疊加的效果。例如,下面的示例繪制一個(gè)紅色矩形,然后在不遮掩底層的紅色矩形的情況下疊加一個(gè)黃色矩形。

            Graphics g = pe.Graphics;
g.FillRectangle(new SolidBrush(Color.Red), 600, 350, 100, 100);
g.FillRectangle(new SolidBrush(Color.FromArgb(180, Color.Yellow)), 650,
                               400, 100, 100);

          
            Dim g As Graphics = pe.Graphics
g.FillRectangle(new SolidBrush(Color.Red), 600, 350, 100, 100)
g.FillRectangle(new SolidBrush(Color.FromArgb(180, Color.Yellow)), 650, _
                               400, 100, 100)

          
C# VB
使用畫(huà)筆
使用畫(huà)筆填充形狀和路徑的內(nèi)部。例如,若要?jiǎng)?chuàng)建紅色矩形,請(qǐng)參見(jiàn)下面的示例。

            Graphics g = pe.Graphics;
g.FillRectangle(new SolidBrush(Color.Red), 600, 350, 100, 100);

          
            Dim g As Graphics = pe.Graphics
g.FillRectangle(new SolidBrush(Color.Red), 600, 350, 100, 100)

          
C# VB

畫(huà)筆可以是純色的、帶陰影的、帶紋理的或漸變的。陰影畫(huà)筆只不過(guò)是使用圖案進(jìn)行繪畫(huà)的畫(huà)筆。

            Graphics g = pe.Graphics;
HatchBrush hb = new HatchBrush(HatchStyle.ForwardDiagonal, Color.Green,
                               Color.FromArgb(100, Color.Yellow));
g.FillEllipse(hb, 250, 10, 100, 100);

          
            Dim g As Graphics = pe.Graphics
HatchBrush hb = new HatchBrush(HatchStyle.ForwardDiagonal, Color.Green, _
                               Color.FromArgb(100, Color.Yellow))
g.FillEllipse(hb, 250, 10, 100, 100)

          
C# VB

帶紋理的畫(huà)筆使用位圖繪畫(huà)。例如,下面的代碼使用紋理畫(huà)筆繪制窗體的背景,然后對(duì)它應(yīng)用白色"沖洗"以沖淡位圖中顏色的色調(diào)。

            Graphics g = pe.Graphics;
Image colorbars = new Bitmap("colorbars.jpg");
Brush backgroundBrush = new TextureBrush(colorbars);
g.FillRectangle(backgroundBrush, ClientRectangle);
g.FillRectangle(new SolidBrush(Color.FromArgb(180, Color.White)), ClientRectangle);

          
            Dim g As Graphics = pe.Graphics
Dim colorbars As Image = new Bitmap("colorbars.jpg")
Dim backgroundBrush As Brush = new TextureBrush(colorbars)
g.FillRectangle(backgroundBrush, ClientRectangle)
g.FillRectangle(new SolidBrush(Color.FromArgb(180, Color.White)), ClientRectangle)

          
C# VB

LinearGradientBrush 使用兩種顏色繪畫(huà)。它根據(jù)在畫(huà)筆上設(shè)置的屬性填充給定的形狀,逐漸從一種顏色過(guò)渡到另一種顏色。例如,可以用下面所示代碼創(chuàng)建下列填充效果。



            Rectangle r = new Rectangle(500, 300, 100, 100);
LinearGradientBrush lb = new LinearGradientBrush(r, Color.Red, Color.Yellow,
                                            LinearGradientMode.BackwardDiagonal);
e.Graphics.FillRectangle(lb, r);

          
            Dim r As Rectangle = new Rectangle(500, 300, 100, 100);
Dim lb As LinearGradientBrush = new LinearGradientBrush(r, Color.Red, Color.Yellow, _
                                                       LinearGradientMode.BackwardDiagonal);
e.Graphics.FillRectangle(lb, r);

          
C# VB

PathGradient 畫(huà)筆允許創(chuàng)建更復(fù)雜的效果,如下列形狀。

它是用下面的代碼創(chuàng)建的。

            protected override void OnPaint(PaintEventArgs e) {
    e.Graphics.TextRenderingHint = TextRenderingHint.AntiAliased;
    e.Graphics.FillRectangle(backgroundBrush, ClientRectangle);
    e.Graphics.FillRectangle(new SolidBrush(Color.FromArgb(180, Color.White)), ClientRectangle);

    GraphicsPath path = new GraphicsPath(new Point[] {
        new Point(40, 140),
        new Point(275, 200),
        new Point(105, 225),
        new Point(190, 300),
        new Point(50, 350),
        new Point(20, 180),
        }, new byte[] {
            (byte)PathPointType.Start,
            (byte)PathPointType.Bezier,
            (byte)PathPointType.Bezier,
            (byte)PathPointType.Bezier,
            (byte)PathPointType.Line,
            (byte)PathPointType.Line,
            });

    PathGradientBrush pgb = new PathGradientBrush(path);
    pgb.SurroundColors = new Color[] {
        Color.Green,
        Color.Yellow,
        Color.Red,
        Color.Blue,
        Color.Orange,
        Color.White,
    };

    e.Graphics.FillPath(pgb, path);
}

          
            Protected Overrides Sub OnPaint(e As PaintEventArgs)
    e.Graphics.TextRenderingHint = TextRenderingHint.AntiAliased
    e.Graphics.FillRectangle(backgroundBrush, ClientRectangle)
    e.Graphics.FillRectangle(New SolidBrush(Color.FromArgb(180, Color.White)), ClientRectangle)

    Dim path As New GraphicsPath(New Point() { _
        New Point(40, 140), _
        New Point(275, 200), _
        New Point(105, 225), _
        new Point(190, 300), _
        new Point(50, 350), _
        new Point(20, 180), _
        }, New Byte() { _
            CType(PathPointType.Start, Byte), _
            CType(PathPointType.Bezier, Byte), _
            CType(PathPointType.Bezier, Byte), _
            CType(PathPointType.Bezier, Byte), _
            CType(PathPointType.Line, Byte), _
            CType(PathPointType.Line, Byte), _
            })

    Dim pgb As New PathGradientBrush(path)
    pgb.SurroundColors = new Color() { _
        Color.Green, _
        Color.Yellow, _
        Color.Red, _
        Color.Blue, _
        Color.Orange, _
        Color.White, _
    }

    e.Graphics.FillPath(pgb, path)
End Sub

          
C# VB



<nobr><a target="_top"><img src="http://chs.gotdotnet.com/quickstart/winforms/images/wflink.jpg" border="1" alt=""><br></a> <table><tbody><tr> <td class="caption">VB 畫(huà)筆示例</td> </tr></tbody></table> <br>[<a target="_top">運(yùn)行示例</a>] | [<a target="_blank">查看源代碼</a>] </nobr>

使用鋼筆

使用鋼筆繪制直線(xiàn)和曲線(xiàn)。可以設(shè)置屬性,如 PenType DashStyle Width Color EndCap ,以控制 Pen 如何進(jìn)行繪制。

下面的代碼繪制一條曲線(xiàn)。

            Graphics g;
//Create a pen 20 pixels wide that is and purple and partially transparent. 
Pen penExample = new Pen(Color.FromArgb(150, Color.Purple), 20);
//Make it a dashed pen.
Pen penExample.DashStyle = DashStyle.Dash;
//Make the ends round.
Pen penExample.StartCap = LineCap.Round;
Pen penExample.EndCap = LineCap.Round;

//Now draw a curve using the pen. 
g.DrawCurve(penExample, new Point[] {
            new Point(200, 140),
            new Point(700, 240),
            new Point(500, 340),
            new Point(140, 140),
            new Point(40, 340),
});

          
            Dim g As Graphics
' Create a pen 20 pixels wide that is and purple and partially transparent.
Dim penExample As New Pen(Color.FromArgb(150, Color.Purple), 20)
' Make it a dashed pen.
Dim penExample.DashStyle As Pen = DashStyle.Dash
' Make the ends round.
Dim penExample.StartCap As Pen = LineCap.Round
Dim penExample.EndCap As Pen= LineCap.Round

' Now draw a curve using the pen
g.DrawCurve(penExample, New Point() { _
            New Point(200, 140), _
            New Point(700, 240), _
            New Point(500, 340), _
            New Point(140, 140), _
            New Point(40, 340), _
})

          
C# VB

還可以使用帶紋理的畫(huà)筆將紋理用作鋼筆的填充效果。

            Graphics g;
Brush textureBrush = new TextureBrush(new Bitmap("Boiling Point.jpg"));
Pen penExample = new Pen(textureBrush, 25);
penExample.DashStyle = DashStyle.DashDotDot;
penExample.StartCap = LineCap.Triangle;
penExample.EndCap = LineCap.Round;
g.DrawLine(penExample, 10,450,550,400);

          
            Dim g As Graphics
Dim textureBrush As New TextureBrush(New Bitmap("Boiling Point.jpg"))
Dim penExample As New Pen(textureBrush, 25)
penExample.DashStyle = DashStyle.DashDotDot
penExample.StartCap = LineCap.Triangle
penExample.EndCap = LineCap.Round
g.DrawLine(penExample, 10,450,550,400)

          
C# VB



<nobr><a target="_top"><img src="http://chs.gotdotnet.com/quickstart/winforms/images/wflink.jpg" border="1" alt=""><br></a> <table><tbody><tr> <td class="caption">VB 筆示例</td> </tr></tbody></table> <br>[<a target="_top">運(yùn)行示例</a>] | [<a target="_blank">查看源代碼</a>] </nobr>

繪制文本

Graphics 對(duì)象的 DrawString 方法在繪圖表面上呈現(xiàn)文本。將要使用的字體和顏色傳遞給 DrawString 方法。例如,下面的代碼使用窗體的字體和黑色畫(huà)筆顯示文本"Hello World"。

            protected override void OnPaint(PaintEventArgs e) {
    Graphics g = e.Graphics;
    e.Graphics.FillRectangle(new SolidBrush(Color.White), ClientRectangle);
    g.DrawString("Hello World", this.Font, new SolidBrush(Color.Black), 10,10);
}

          
            Protected Overrides Sub OnPaint(e As PaintEventArgs)
    Dim g As Graphics = e.Graphics
    e.Graphics.FillRectangle(New SolidBrush(Color.White), ClientRectangle)
    g.DrawString("Hello World", Me.Font, New SolidBrush(Color.Black), 10,10)
End Sub

          
C# VB

因?yàn)? DrawString 采用畫(huà)筆,所以可以使用任何畫(huà)筆呈現(xiàn)文本。其中包括紋理畫(huà)筆。例如,下面的代碼呈現(xiàn)具有大理石效果和背景陰影的文本。

            protected override void OnPaint(PaintEventArgs e) {
    TextureBrush titleBrush = new TextureBrush(new Bitmap("marble.jpg"));
    TextureBrush backgroundBrush = new TextureBrush(new Bitmap("colorbars.jpg"));
    SolidBrush titleShadowBrush = new SolidBrush(Color.FromArgb(70, Color.Black));
    Font titleFont = new Font("Lucida Sans Unicode", 60);
    string titleText = "Graphics  Samples";

    e.Graphics.TextRenderingHint = TextRenderingHint.AntiAliased;
    e.Graphics.FillRectangle(backgroundBrush, ClientRectangle);
    e.Graphics.FillRectangle(new SolidBrush(Color.FromArgb(180, Color.White)), ClientRectangle);

    e.Graphics.DrawString(titleText, titleFont, titleShadowBrush, 15, 15);
    e.Graphics.DrawString(titleText, titleFont, titleBrush, 10, 10);

}

          
            Protected Overrides Sub OnPaint(e As PaintEventArgs)
    Dim titleBrush As New TextureBrush(New Bitmap("marble.jpg"))
    Dim backgroundBrush As New TextureBrush(New Bitmap("colorbars.jpg"))
    Dim titleShadowBrush As New SolidBrush(Color.FromArgb(70, Color.Black))
    Dim titleFont As New Font("Lucida Sans Unicode", 60)
    Dim titleText As String = "Graphics  Samples"

    e.Graphics.TextRenderingHint = TextRenderingHint.AntiAliased
    e.Graphics.FillRectangle(backgroundBrush, ClientRectangle)
    e.Graphics.FillRectangle(New SolidBrush(Color.FromArgb(180, Color.White)), ClientRectangle)

    e.Graphics.DrawString(titleText, titleFont, titleShadowBrush, 15, 15)
    e.Graphics.DrawString(titleText, titleFont, titleBrush, 10, 10)

End Sub

          
C# VB

如果提供帶有 Rectangle DrawString ,則文本將換行以適合該矩形。

            protected override void OnPaint(PaintEventArgs e) {
    e.Graphics.TextRenderingHint = TextRenderingHint.AntiAliased;
    e.Graphics.FillRectangle(new SolidBrush(Color.White), ClientRectangle);

    Font textFont = new Font("Lucida Sans Unicode", 12);
    RectangleF rectangle = new RectangleF(100, 100, 250, 350);
    e.Graphics.FillRectangle(new SolidBrush(Color.Gainsboro), rectangle);
    string flowedText="Simplicity and power: Windows Forms is a programming model for\n"
        + "developing Windows applications that combines the simplicity of the Visual\n"
        + "Basic 6.0 programming model with the power and flexibility of the common\n"
        + "language runtime.\n"
        + "Lower total cost of ownership: Windows Forms takes advantage of the versioning and\n"
        + "deployment features of the common language runtime to offer reduced deployment\n"
        + "costs and higher application robustness over time. This significantly lowers the\n"
        + "maintenance costs (TCO) for applications\n"
        + "written in Windows Forms.\n"
        + "Architecture for controls: Windows Forms offers an architecture for\n"
        + "controls and control containers that is based on concrete implementation of the\n"
        + "control and container classes. This significantly reduces\n"
        + "control-container interoperability issues.\n";
    e.Graphics.DrawString(flowedText, textFont, new SolidBrush(Color.Blue), rectangle);
}

          
            Protected Overrides Sub OnPaint(e As PaintEventArgs)
    e.Graphics.TextRenderingHint = TextRenderingHint.AntiAliased
    e.Graphics.FillRectangle(New SolidBrush(Color.White), ClientRectangle)

    Dim textFont As New Font("Lucida Sans Unicode", 12)
    Dim rectangle As New RectangleF(100, 100, 250, 350)
    e.Graphics.FillRectangle(New SolidBrush(Color.Gainsboro), rectangle)
    Dim flowedText As String ="Simplicity and power: Windows Forms is a programming model for" & ControlChars.CrLf _
        & "developing Windows applications that combines the simplicity of the Visual" & ControlChars.CrLf _
        & "Basic 6.0 programming model with the power and flexibility of the common" & ControlChars.CrLf _
        & "language runtime." & ControlChars.CrLf _
        & "Lower total cost of ownership: Windows Forms takes advantage of the versioning and" & ControlChars.CrLf _
        & "deployment features of the common language runtime to offer reduced deployment" & ControlChars.CrLf _
        & "costs and higher application robustness over time. This significantly lowers the" & ControlChars.CrLf _
        & "maintenance costs (TCO) for applications" & ControlChars.CrLf _
        & "written in Windows Forms." & ControlChars.CrLf _
        & "Architecture for controls: Windows Forms offers an architecture for" & ControlChars.CrLf _
        & "controls and control containers that is based on concrete implementation of the" & ControlChars.CrLf _
        & "control and container classes. This significantly reduces" & ControlChars.CrLf _
        & "control-container interoperability issues." & ControlChars.CrLf
    e.Graphics.DrawString(flowedText, textFont, New SolidBrush(Color.Blue), rectangle)
End Sub

          
C# VB

可以使用 StringFormat 對(duì)象控制如何繪制文本。例如,下面的代碼使您得以繪制在特定區(qū)域內(nèi)居中的文本。

            protected override void OnPaint(PaintEventArgs e) {
    e.Graphics.TextRenderingHint = TextRenderingHint.AntiAliased;
    e.Graphics.FillRectangle(new SolidBrush(Color.White), ClientRectangle);

    Font textFont = new Font("Lucida Sans Unicode", 8);
    RectangleF rectangle = new RectangleF(100, 100, 250, 350);
    e.Graphics.FillRectangle(new SolidBrush(Color.Gainsboro), rectangle);
    StringFormat format = new StringFormat();
    format.Alignment=StringAlignment.Center;
    string flowedText="Simplicity and power: Windows Forms is a programming model for\n"
        + "developing Windows applications that combines the simplicity of the Visual\n"
        + "Basic 6.0 programming model with the power and flexibility of the common\n"
        + "language runtime.\n"
        + "Lower total cost of ownership: Windows Forms takes advantage of the versioning and\n"
        + "deployment features of the common language runtime to offer reduced deployment\n"
        + "costs and higher application robustness over time. This significantly lowers the\n"
        + "maintenance costs (TCO) for applications\n"
        + "written in Windows Forms.\n"
        + "Architecture for controls: Windows Forms offers an architecture for\n"
        + "controls and control containers that is based on concrete implementation of the\n"
        + "control and container classes. This significantly reduces\n"
        + "control-container interoperability issues.\n";
    e.Graphics.DrawString(flowedText, textFont, new SolidBrush(Color.Blue), rectangle,format);
}

          
            Protected Overrides Sub OnPaint(e As PaintEventArgs)
    e.Graphics.TextRenderingHint = TextRenderingHint.AntiAliased
    e.Graphics.FillRectangle(New SolidBrush(Color.White), ClientRectangle)

    Dim textFont As New Font("Lucida Sans Unicode", 8)
    Dim rectangle As New RectangleF(100, 100, 250, 350)
    e.Graphics.FillRectangle(new SolidBrush(Color.Gainsboro), rectangle)
    Dim format As New StringFormat()
    format.Alignment = StringAlignment.Center
    Dim flowedText As String ="Simplicity and power: Windows Forms is a programming model for" & ControlChars.CrLf _
        & "developing Windows applications that combines the simplicity of the Visual" & ControlChars.CrLf _
        & "Basic 6.0 programming model with the power and flexibility of the common" & ControlChars.CrLf _
        & "language runtime." & ControlChars.CrLf _
        & "Lower total cost of ownership: Windows Forms takes advantage of the versioning and" & ControlChars.CrLf _
        & "deployment features of the common language runtime to offer reduced deployment" & ControlChars.CrLf _
        & "costs and higher application robustness over time. This significantly lowers the" & ControlChars.CrLf _
        & "maintenance costs (TCO) for applications" & ControlChars.CrLf _
        & "written in Windows Forms." & ControlChars.CrLf _
        & "Architecture for controls: Windows Forms offers an architecture for" & ControlChars.CrLf _
        & "controls and control containers that is based on concrete implementation of the" & ControlChars.CrLf _
        & "control and container classes. This significantly reduces" & ControlChars.CrLf _
        & "control-container interoperability issues." & ControlChars.CrLf
    e.Graphics.DrawString(flowedText, textFont, New SolidBrush(Color.Blue), rectangle,format)
End Sub

          
C# VB

如果要在繪制字符串時(shí)確定該字符串的長(zhǎng)度,可使用 MeasureString 。例如,若要將某個(gè)字符串在窗體上居中顯示,請(qǐng)使用下面的代碼。

            protected override void OnPaint(PaintEventArgs e) {
    e.Graphics.TextRenderingHint = TextRenderingHint.AntiAliased;
    e.Graphics.FillRectangle(new SolidBrush(Color.White), ClientRectangle);

    string textToDraw="Hello Symetrical World";

    Font textFont = new Font("Lucida Sans Unicode", 8);
    float windowCenter=this.DisplayRectangle.Width/2;
    SizeF stringSize=e.Graphics.MeasureString(textToDraw, textFont);
    float startPos=windowCenter-(stringSize.Width/2);

    e.Graphics.DrawString(textToDraw, textFont, new SolidBrush(Color.Blue), startPos, 40);
}

          
            Protected Overrides Sub OnPaint(e As PaintEventArgs)
    e.Graphics.TextRenderingHint = TextRenderingHint.AntiAliased
    e.Graphics.FillRectangle(new SolidBrush(Color.White), ClientRectangle)

    Dim textToDraw As String ="Hello Symetrical World"

    Dim textFont As New Font("Lucida Sans Unicode", 8)
    Dim windowCenter As Double = this.DisplayRectangle.Width/2
    Dim stringSize As SizeF = e.Graphics.MeasureString(textToDraw, textFont)
    Dim startPos As Double = windowCenter-(stringSize.Width/2)

    e.Graphics.DrawString(textToDraw, textFont, new SolidBrush(Color.Blue), startPos, 40)
End Sub

          
C# VB

MeasureString 還可用于確定呈現(xiàn)多少行和多少個(gè)字符。例如,我們可以確定在上一個(gè)討論過(guò)的文本示例中呈現(xiàn)多少行和多少個(gè)字符。

            protected override void OnPaint(PaintEventArgs e) {
    e.Graphics.TextRenderingHint = TextRenderingHint.AntiAliased;
    e.Graphics.FillRectangle(new SolidBrush(Color.White), ClientRectangle);

    Font textFont = new Font("Lucida Sans Unicode", 12);
    RectangleF rectangle = new RectangleF(100, 100, 250, 350);
    int lines;
    int characters;
    string flowedText="Simplicity and power: Windows Forms is a programming model for\n"
        + "developing Windows applications that combines the simplicity of the Visual\n"
        + "Basic 6.0 programming model with the power and flexibility of the common\n"
        + "language runtime.\n"
        + "Lower total cost of ownership: Windows Forms takes advantage of the versioning and\n"
        + "deployment features of the common language runtime to offer reduced deployment\n"
        + "costs and higher application robustness over time. This significantly lowers the\n"
        + "maintenance costs (TCO) for applications\n"
        + "written in Windows Forms.\n"
        + "Architecture for controls: Windows Forms offers an architecture for\n"
        + "controls and control containers that is based on concrete implementation of the\n"
        + "control and container classes. This significantly reduces\n"
        + "control-container interoperability issues.\n";
    string whatRenderedText;

    e.Graphics.FillRectangle(new SolidBrush(Color.Gainsboro), rectangle);

    e.Graphics.MeasureString(flowedText, textFont, rectangle.Size,
                    new StringFormat(), out characters, out lines);
    whatRenderedText="We printed " + characters + " characters and " + lines + " lines";

    e.Graphics.DrawString(flowedText, textFont, new SolidBrush(Color.Blue), rectangle);

    e.Graphics.DrawString(whatRenderedText, this.Font, new SolidBrush(Color.Black), 10,10);
}

          
            Protected Overrides Sub OnPaint(e As PaintEventArgs)
    e.Graphics.TextRenderingHint = TextRenderingHint.AntiAliased
    e.Graphics.FillRectangle(new SolidBrush(Color.White), ClientRectangle)

    Dim textFont As New Font("Lucida Sans Unicode", 12)
    Dim rectangle As New RectangleF(100, 100, 250, 350)
    Dim lines As Integer
    Dim characters As Integer
    Dim flowedText As String ="Simplicity and power: Windows Forms is a programming model for" & ControlChars.CrLf _
        & "developing Windows applications that combines the simplicity of the Visual" & ControlChars.CrLf _
        & "Basic 6.0 programming model with the power and flexibility of the common" & ControlChars.CrLf _
        & "language runtime." & ControlChars.CrLf _
        & "Lower total cost of ownership: Windows Forms takes advantage of the versioning and" & ControlChars.CrLf _
        & "deployment features of the common language runtime to offer reduced deployment" & ControlChars.CrLf _
        & "costs and higher application robustness over time. This significantly lowers the" & ControlChars.CrLf _
        & "maintenance costs (TCO) for applications" & ControlChars.CrLf _
        & "written in Windows Forms." & ControlChars.CrLf _
        & "Architecture for controls: Windows Forms offers an architecture for" & ControlChars.CrLf _
        & "controls and control containers that is based on concrete implementation of the" & ControlChars.CrLf _
        & "control and container classes. This significantly reduces" & ControlChars.CrLf _
        & "control-container interoperability issues." & ControlChars.CrLf
    Dim whatRenderedText As String

    e.Graphics.FillRectangle(New SolidBrush(Color.Gainsboro), rectangle)

    e.Graphics.MeasureString(flowedText, textFont, rectangle.Size, _
                    new StringFormat(), characters, lines)
    whatRenderedText = "We printed " & characters & " characters and " & lines & " lines"

    e.Graphics.DrawString(flowedText, textFont, New SolidBrush(Color.Blue), rectangle)

    e.Graphics.DrawString(whatRenderedText, Me.Font, New SolidBrush(Color.Black), 10,10)
End Sub

          
C# VB

GDI+ 完全支持 Unicode。這意味著可以用任何語(yǔ)言呈現(xiàn)文本。例如,下面的代碼用日語(yǔ)繪制字符串(必須安裝有日語(yǔ)語(yǔ)言包)。

            protected override void OnPaint(PaintEventArgs e) {
    e.Graphics.TextRenderingHint = TextRenderingHint.AntiAliased;
    e.Graphics.FillRectangle(new SolidBrush(Color.White), ClientRectangle);

    try {
        Font japaneseFont = new Font("MS Mincho", 32);
        string japaneseText = new string(new char[] {(char)31169, (char)12398, (char)21517,
                    (char)21069, (char)12399, (char)12463, (char)12522, (char)12473,
                    (char)12391, (char)12377, (char)12290});

        e.Graphics.DrawString(japaneseText, japaneseFont, new SolidBrush(Color.Blue), 20, 40);
    } catch (Exception ex) {
         MessageBox.Show("You need to install the Japanese language pack to run this sample");
         Application.Exit();
    }
}

          
            Protected Overrides Sub OnPaint(e As PaintEventArgs)
    e.Graphics.TextRenderingHint = TextRenderingHint.AntiAliased
    e.Graphics.FillRectangle(new SolidBrush(Color.White), ClientRectangle)

    Try
        Dim japaneseFont As New Font("MS Mincho", 32)
        Dim japaneseText As New String(new char() {CType(31169, Char), CType(12398, Char), _
                    CType(21517, Char), CType(21069, Char), CType(12399, Char), _
                    CType(12463, Char), CType(12522, Char), CType(12473, Char), _
                    CType(12391, Char), CType(12377, Char), CType(12290, Char)})
        e.Graphics.DrawString(japaneseText, japaneseFont, new SolidBrush(Color.Blue), 20, 40)
    Catch ex As Exception
         MessageBox.Show("You need to install the Japanese language pack to run this sample")
         Application.Exit()
    End Try
End Sub

          
C# VB


<nobr><a target="_top"><img src="http://chs.gotdotnet.com/quickstart/winforms/images/wflink.jpg" border="1" alt=""><br></a> <table><tbody><tr> <td class="caption">VB 文本示例</td> </tr></tbody></table> <br>[<a target="_top">運(yùn)行示例</a>] | [<a target="_blank">查看源代碼</a>] </nobr>

使用圖像

GDI+ 完全支持各種圖像格式,包括 .jpeg、.png、.gif、.bmp、.tiff、.exif 和 .icon 文件。

呈現(xiàn)圖像非常簡(jiǎn)單。例如,下面的代碼呈現(xiàn)一個(gè) .jpeg 圖像。

            protected override void OnPaint(PaintEventArgs e) {
    e.Graphics.FillRectangle(new SolidBrush(Color.White), ClientRectangle);
    e.Graphics.DrawImage(new Bitmap("sample.jpg"), 29, 20, 283, 212);
}

          
            Protected Overrides Sub OnPaint(e As PaintEventArgs)
    e.Graphics.FillRectangle(New SolidBrush(Color.White), ClientRectangle)
    e.Graphics.DrawImage(New Bitmap("sample.jpg"), 29, 20, 283, 212)
End Sub

          
C# VB

使用位圖作為呈現(xiàn)圖面也非常簡(jiǎn)單。下面的代碼將一些文本和線(xiàn)呈現(xiàn)到位圖上,然后將該位圖作為 .png 文件保存到磁盤(pán)中。

            Bitmap newBitmap = new Bitmap(800,600,PixelFormat.Format32bppArgb);
Graphics g = Graphics.FromImage(newBitmap);
g.FillRectangle(new SolidBrush(Color.White), new Rectangle(0,0,800,600));

Font textFont = new Font("Lucida Sans Unicode", 12);
RectangleF rectangle = new RectangleF(100, 100, 250, 350);
int lines;
int characters;
string flowedText="Simplicity and power: Windows Forms is a programming model for\n"
    + "developing Windows applications that combines the simplicity of the Visual\n"
    + "Basic 6.0 programming model with the power and flexibility of the common\n"
    + "language runtime.\n"
    + "Lower total cost of ownership: Windows Forms takes advantage of the versioning and\n"
    + "deployment features of the common language runtime to offer reduced deployment\n"
    + "costs and higher application robustness over time. This significantly lowers the\n"
    + "maintenance costs (TCO) for applications\n"
    + "written in Windows Forms.\n"
    + "Architecture for controls: Windows Forms offers an architecture for\n"
    + "controls and control containers that is based on concrete implementation of the\n"
    + "control and container classes. This significantly reduces\n"
    + "control-container interoperability issues.\n";

g.FillRectangle(new SolidBrush(Color.Gainsboro), rectangle);
g.DrawString(flowedText, textFont, new SolidBrush(Color.Blue), rectangle);
Pen penExample = new Pen(Color.FromArgb(150, Color.Purple), 20);
penExample.DashStyle = DashStyle.Dash;
penExample.StartCap = LineCap.Round;
penExample.EndCap = LineCap.Round;
g.DrawCurve(penExample, new Point[] {
            new Point(200, 140),
            new Point(700, 240),
            new Point(500, 340),
            new Point(140, 140),
            new Point(40, 340),
});

newBitmap.Save("TestImage.png", ImageFormat.Png) ;

          
            Dim newBitmap As New Bitmap(800,600,PixelFormat.Format32bppArgb)
Dim g As Graphics = Graphics.FromImage(newBitmap)
g.FillRectangle(New SolidBrush(Color.White), new Rectangle(0,0,800,600))

Dim textFont As New Font("Lucida Sans Unicode", 12)
Dim rectangle As New RectangleF(100, 100, 250, 350)
Dim Lines As Integer
Dim Characters As Integer
Dim flowedText As String ="Simplicity and power: Windows Forms is a programming model for" & ControlChars.CrLf _
    & "developing Windows applications that combines the simplicity of the Visual" & ControlChars.CrLf _
    & "Basic 6.0 programming model with the power and flexibility of the common" & ControlChars.CrLf _
    & "language runtime." & ControlChars.CrLf _
    & "Lower total cost of ownership: Windows Forms takes advantage of the versioning and" & ControlChars.CrLf _
    & "deployment features of the common language runtime to offer reduced deployment" & ControlChars.CrLf _
    & "costs and higher application robustness over time. This significantly lowers the" & ControlChars.CrLf _
    & "maintenance costs (TCO) for applications" & ControlChars.CrLf _
    & "written in Windows Forms." & ControlChars.CrLf _
    & "Architecture for controls: Windows Forms offers an architecture for" & ControlChars.CrLf _
    & "controls and control containers that is based on concrete implementation of the" & ControlChars.CrLf _
    & "control and container classes. This significantly reduces" & ControlChars.CrLf _
    & "control-container interoperability issues." & ControlChars.CrLf

g.FillRectangle(new SolidBrush(Color.Gainsboro), rectangle)
g.DrawString(flowedText, textFont, new SolidBrush(Color.Blue), rectangle)
Dim penExample As New Pen(Color.FromArgb(150, Color.Purple), 20)
penExample.DashStyle = DashStyle.Dash
penExample.StartCap = LineCap.Round
penExample.EndCap = LineCap.Round
g.DrawCurve(penExample, new Point() { _
            new Point(200, 140), _
            new Point(700, 240), _
            new Point(500, 340), _
            new Point(140, 140), _
            new Point(40, 340), _
})

newBitmap.Save("TestImage.png", ImageFormat.Png)

          
C# VB



<nobr><a target="_top"><img src="http://chs.gotdotnet.com/quickstart/winforms/images/wflink.jpg" border="1" alt=""><br></a> <table><tbody><tr> <td class="caption">VB 圖像示例</td> </tr></tbody></table> <br>[<a target="_top">運(yùn)行示例</a>] | [<a target="_blank">查看源代碼</a>] </nobr>

其他信息
標(biāo)準(zhǔn)鋼筆和畫(huà)筆

Pen Brush 類(lèi)包括一組用于所有已知顏色的標(biāo)準(zhǔn)純色鋼筆和畫(huà)筆。

消除鋸齒
Graphics.SmoothingMode 設(shè)置為 SmoothingMode.AntiAlias 將導(dǎo)致更銳化的文本和圖形。
圖形對(duì)象的范圍
Paint 事件的參數(shù) ( PaintEventArgs ) 中包含的圖形對(duì)象根據(jù) Paint 事件處理程序的返回結(jié)果進(jìn)行處置。因此,不應(yīng)保持對(duì)范圍超出 Paint 事件之外的此 Graphics 對(duì)象的引用。嘗試在 Paint 事件之外使用此 Graphics 對(duì)象將出現(xiàn)不可預(yù)知的結(jié)果。
處理調(diào)整大小

默認(rèn)情況下,當(dāng)調(diào)整控件或窗體大小時(shí)不引發(fā) Paint 事件。如果希望在調(diào)整窗體大小時(shí)引發(fā) Paint 事件,則需要相應(yīng)地設(shè)置控件樣式。

            public MyForm() {
    SetStyle(ControlStyles.ResizeRedraw,true);
}

          
            Public Sub MyForm()
    SetStyle(ControlStyles.ResizeRedraw,True)
End Sub

          
C# VB

轉(zhuǎn)自 http://www.gotdotnet.com/

PrintDocument PrintPage 事件中獲取對(duì)圖形對(duì)象的引用。

圖形和 Windows 窗體


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號(hào)聯(lián)系: 360901061

您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對(duì)您有幫助就好】

您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長(zhǎng)會(huì)非常 感謝您的哦!!!

發(fā)表我的評(píng)論
最新評(píng)論 總共0條評(píng)論
主站蜘蛛池模板: 日韩精品久久久毛片一区二区 | 久久精品久久久久久久久人 | 久热综合 | www.久操| 免费观看日本污污ww网站一区 | 欧美精品一区二区精品久久 | 中国男女全黄大片一级 | 亚洲线精品一区二区三区 | 欧美成人综合 | 国内精品久久久久影 | 国产欧美乱码在线看 | xx色综合| 免费 黄 色 人成 视频 | 中日韩欧美中文字幕毛片 | 精品天海翼一区二区 | 成人短视频在线观看 | 毛片大全高清免费 | 欧美日韩一区二区三区毛片 | 黄色网址在线免费 | 国产免费一区二区三区香蕉精 | 国产精自产拍久久久久久 | 欧美日本中文字幕 | 日本护士一级毛片在线播放 | 精品伊人久久大香线蕉网站 | 中文字幕一区精品 | 69国产成人综合久久精品 | 色婷婷中文字幕 | 亚洲精品国产成人 | 综合在线视频精品专区 | 狠狠色噜噜狠狠狠狠888奇米 | 欧美色视频网 | 久久精品2| 无码免费一区二区三区免费播放 | 欧美特级黄色大片 | 国产精品久久自在自2021 | 日本欧美一区二区三区乱码 | 国产精品久久免费 | 久久99热成人精品国产 | 97超精品视频在线观看 | 日韩在线中文 | 午夜精品福利影院 |