成人午夜激情影院,小视频免费在线观看,国产精品夜夜嗨,欧美日韩精品一区二区在线播放

.NET Compact Framework圖形開發常見問題

2010-08-28 10:49:44來源:西部e網作者:

1. 如何創建 Graphics 對象?

圖形對象根據其用途有幾種創建方式:

通過 OnPaint,使用 PaintEventArgs 中提供的對象:

//C#

protected override void OnPaint(PaintEventArgs e)
{
  e.Graphics.DrawLine(...);
}

'VB

Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
    e.Graphics.DrawLine(...)
End Sub 'OnPaint

通過代碼中的其他區域,該函數是 Control 的一個方法,可以用來創建任何控件的圖形對象:

//C#

using System.Drawing;

Graphics g = this.CreateGraphics();

'VB

Imports System.Drawing

Dim g As Graphics = Me.CreateGraphics()

直接在一個位圖中繪制:

//C#

using System.Drawing;

Bitmap bm = new Bitmap(10,10);
Graphics g = Graphics.FromImage(bm);

'VB

Imports System.Drawing

Dim bm As New Bitmap(10, 10)
Dim g As Graphics = Graphics.FromImage(bm)

2. 可以怎樣優化 GDI+ 呈現?

當使用 Graphics 繪畫調用時,有幾種基本的編碼實踐可以幫助提高繪畫速度:

只創建一個 Graphics 對象(或者使用來自 OnPaint 中的 PaintEventArgs 的 Graphics 對象)。

在屏幕外的位圖中完成所有繪畫,然后拖曳該位圖,使它一次全部顯示出來。

只重畫圖像的更改部分。

盡可能使繪制的源和目的大小相同(不要拉伸等等)。

也許最重要的實踐是保留需要重畫的項目的痕跡以便使出現的繪畫量盡可能少。例如,如果拖曳光標跨過圖像,就不需要重畫整個圖像。相反,只需重畫前一個光標位置覆蓋的圖像部分。

3. 如何在窗體中繪制圖像?

此示例顯示了如何將一個圖形作為窗體的背景圖像顯示:
http://samples.gotdotnet.com/quickstart/CompactFramework/doc/bkgndimage.aspx

4. 如何繪制具有透明度的圖像?

繪制有透明度的圖像需要一個指定透明顏色的 ImageAttributes 對象。當前,.NET Compact Framework 支持單種顏色的原色調透明度。雖然 SetColorKey 函數允許一個顏色范圍,但最小和最大的顏色必須相同,否則會產生運行時 ArgumentException:

//C#

using System.Drawing.Imaging;

ImageAttributes attr = new ImageAttributes();

'VB

Imports System.Drawing.Imaging

Dim attr As New ImageAttributes()

以下代碼演示了如何根據圖像的左上像素設置透明色調。

//C#

attr.SetColorKey(bmp.GetPixel(0,0), bmp.GetPixel(0,0));

'VB

attr.SetColorKey(bmp.GetPixel(0,0), bmp.GetPixel(0,0))

也可以按照如下所述方法顯式設置顏色:

//C#

attr.SetColorKey(Color.FromArgb(255,0,255),Color.FromArgb(255,0,255));
attr.SetColorKey(Color.Fuchsia, Color.Fuchsia);

'VB

attr.SetColorKey(Color.FromArgb(255,0,255),Color.FromArgb(255,0,255))
attr.SetColorKey(Color.Fuchsia, Color.Fuchsia)

然后可以用重載的 Graphics.DrawImage 函數(它將 ImageAttributes 對象作為一個參數)來繪制圖像:

//C#

Rectangle dstRect = new Rectangle(0, 0, bmp.Width, bmp.Height);
g.DrawImage(bmp, dstRect, 0, 0, bmp.Width, bmp.Height, GraphicsUnit.Pixel, attr);

'VB

Dim dstRect As New Rectangle(0, 0, bmp.Width, bmp.Height)
g.DrawImage(bmp, dstRect, 0, 0, bmp.Width, bmp.Height, GraphicsUnit.Pixel, attr)

5. 當我在 TextBox 中調用 CreateGraphics 時它為什么會失敗?

只有 Control 和 Form 類支持 Control.CreateGraphics()。

6. 如何確定文本的屏幕大小?

使用 Graphics 方法 MeasureString。以下代碼演示了如何在一些文本周圍繪制一個框:

//C#

using System.Drawing;

protected override void OnPaint(PaintEventArgs e)
{
  string s = "Hello World"

  Pen pen = new Pen(Color.Fuchsia);
  Font font = new Font("Arial", 18, FontStyle.Regular);
  Brush brush = new SolidBrush(Color.Black);

  SizeF sSize = e.Graphics.MeasureString(s, font);

  Rectangle r = new Rectangle(9, 199,(int)sSize.Width + 1, (int)sSize.Height + 1);

  e.Graphics.DrawRectangle(pen, r);
  e.Graphics.DrawString(s, font, brush, 10.0f, 200.0f);

  base.OnPaint (e);
}

'VB

Imports System.Drawing

Protected Overrides Sub OnPaint(e As PaintEventArgs)
  Dim s As String = "Hello World"
  
  Dim pen As New Pen(Color.Fuchsia)
  Dim font As New Font("Arial", 18, FontStyle.Regular)
  Dim brush = New SolidBrush(Color.Black)

  Dim sSize As SizeF = e.Graphics.MeasureString(s, font)

  Dim r As New Rectangle(9, 199, Fix(sSize.Width) + 1, Fix(sSize.Height) + 1)

  e.Graphics.DrawRectangle(pen, r)
  e.Graphics.DrawString(s, font, brush, 10F, 200F)

  MyBase.OnPaint(e)

End Sub 'OnPaint

7. 可以設置畫筆的寬度嗎?

.NET Compact Framework 中不可以設置畫筆寬度。有一些替代辦法,包括:

采用 Graphics.FillRectangle 方法繪制實心矩形

繪制多條并行線

采用 GAPI 編寫自定義圖形例程

8. 如何縮放圖像?

雖然沒有對縮放和拉伸單個圖像的內在支持,但這些效果也可以很輕松地實現,方法是使用相關的 Graphics 對象創建新的 Bitmap 對象,然后將原有 Bitmap 想要的部分復制到新建對象上。以下示例創建了兩個大小相同的位圖,其中第二個位圖包含第一個位圖經放大的中心部分,并假定項目有一個名為 MyImage.bmp 的嵌入式資源。這樣的技術也可以用來拉伸圖像,方法是修改源和目的矩形以便它們沒有保留其原有的縱橫比。

//C#

using System.Drawing;
using System.Reflection;

Bitmap m_bmpOriginal;
Bitmap m_bmpZoom;

private void Form1_Load(object sender, System.EventArgs e)
{
    Assembly asm = Assembly.GetExecutingAssembly();
    m_bmpOriginal = new Bitmap(asm.GetManifestResourceStream(asm.GetName().Name
      + ".MyImage.bmp"));

    // Take the center quarter of m_bmpOriginal
    // and create stetch it into m_bmpZoom of the same size
    m_bmpZoom = new Bitmap(m_bmpOriginal.Width, m_bmpOriginal.Height);
    Graphics gZoom = Graphics.FromImage(m_bmpZoom);
    
    Rectangle srcRect = new Rectangle(m_bmpOriginal.Width / 4, m_bmpOriginal.Height / 4,
      m_bmpOriginal.Width / 2, m_bmpOriginal.Height / 2);
    Rectangle dstRect = new Rectangle(0, 0, m_bmpZoom.Width, m_bmpZoom.Height);
    gZoom.DrawImage(m_bmpOriginal, dstRect, srcRect, GraphicsUnit.Pixel);
}

protected override void OnPaint(PaintEventArgs e)
{
    e.Graphics.DrawImage(m_bmpOriginal, 0, 0);
    e.Graphics.DrawImage(m_bmpZoom, 125, 0);
    base.OnPaint (e);
}

'VB

Imports System.Drawing
Imports System.Reflection

Private m_bmpOriginal As Bitmap
Private m_bmpZoom As Bitmap

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim asm As [Assembly] = [Assembly].GetExecutingAssembly()
    m_bmpOriginal = New Bitmap(asm.GetManifestResourceStream((asm.GetName().Name _
      + ".MyImage.bmp")))

    ' Take the center quarter of m_bmpOriginal
    ' and create stetch it into m_bmpZoom of the same size
    m_bmpZoom = New Bitmap(m_bmpOriginal.Width, m_bmpOriginal.Height)
    Dim gZoom As Graphics = Graphics.FromImage(m_bmpZoom)

    Dim srcRect As New Rectangle(m_bmpOriginal.Width / 4, m_bmpOriginal.Height / 4, _
      m_bmpOriginal.Width / 2, m_bmpOriginal.Height / 2)
    Dim dstRect As New Rectangle(0, 0, m_bmpZoom.Width, m_bmpZoom.Height)
    gZoom.DrawImage(m_bmpOriginal, dstRect, srcRect, GraphicsUnit.Pixel)
End Sub 'Form1_Load

Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
    e.Graphics.DrawImage(m_bmpOriginal, 0, 0)
    e.Graphics.DrawImage(m_bmpZoom, 125, 0)
    MyBase.OnPaint(e)
End Sub 'OnPaint

9. 為什么我不能加載圖像?

確保 imgdecmp.dll 位于設備的 Windows 目錄。

有關更多信息,請參見本 FAQ 中的主題“ HYPERLINK \l "1.31" 1.31.如何將 imgdemp.dll 包括在模擬器映像中?”。

10. 如何使用 GAPI 創建圖形引擎?

這篇文章描述了如何創建包裝 GAPI (Game API) 的 DLL,使之與 .NET Compact Framework 兼容,并用它來創建和優化托管代碼中的基本圖形庫。
http://msdn.microsoft.com/library/en-us/dnnetcomp/html/WrapGAPI1.asp

這篇文章實現了位圖的加載和顯示,從而擴展了“Dancing Rectangles”示例。它還實現了一些更高級的功能,例如動畫位圖、源和目的色調透明和 alpha 值混合處理(也就是半透明)。
http://msdn.microsoft.com/library/en-us/dnnetcomp/html/WrapGAPI2.asp

這篇文章實現了點、線和從 8 位位圖轉換而來的自定義 1 位字體的繪制,從而擴展了“Dancing Zombies”示例。它還實現了一個重寫硬件按鈕功能和跟蹤按鈕狀態的輸入系統。
http://msdn.microsoft.com/library/en-us/dnnetcomp/html/WrapGAPI3.asp

關鍵詞:dotnet

贊助商鏈接:

主站蜘蛛池模板: 沁水县| 新建县| 石阡县| 临高县| 田林县| 云浮市| 山东| 营口市| 博野县| 太和县| 中山市| 林州市| 开远市| 东台市| 道孚县| 康平县| 来凤县| 日喀则市| 重庆市| 顺义区| 洪雅县| 广东省| 牟定县| 正阳县| 宣汉县| 泸定县| 彭泽县| 连江县| 田阳县| 香港 | 杭州市| 元朗区| 西吉县| 土默特右旗| 尉氏县| 虎林市| 聂荣县| 阿荣旗| 津市市| 姚安县| 锡林郭勒盟|