1. 程式人生 > >C#中的bitmap類的使用方法

C#中的bitmap類的使用方法

封裝 GDI+ 點陣圖,此點陣圖由圖形影象及其特性的畫素資料組成。 Bitmap 是用於處理由畫素資料定義的影象的物件。

名稱空間:  System.Drawing

程式集:  System.Drawing(在 System.Drawing.dll 中)

C#語法

[SerializableAttribute]
[ComVisibleAttribute(true)]
public sealed class Bitmap : Image
Bitmap 型別公開以下成員。
屬性
名稱 說明
公共屬性
Flags 獲取該 Image 的畫素資料的特性標誌。 (繼承自 Image。)
公共屬性 獲取 GUID 的陣列,這些 GUID 表示此 Image 中幀的維數。 (繼承自 Image。)
公共屬性 獲取此 Image 的高度(以畫素為單位)。 (繼承自 Image。)
公共屬性 獲取此 Image 的水平解析度(以“畫素/英寸”為單位)。 (繼承自 Image。)
公共屬性 獲取或設定用於此 Image 的調色盤。 (繼承自 Image。)
公共屬性 獲取此 Image 的畫素格式。 (繼承自 
Image
。)
公共屬性 獲取儲存於該 Image 中的屬性項的 ID。 (繼承自 Image。)
公共屬性 獲取儲存於該 Image 中的所有屬性項(元資料片)。 (繼承自 Image。)
公共屬性 獲取此 Image 的檔案格式。 (繼承自 Image。)
公共屬性 Size 獲取此影象的以畫素為單位的寬度和高度。 (繼承自 Image。)
公共屬性 Tag 獲取或設定提供有關影象附加資料的物件。 (繼承自 Image。)
公共屬性 獲取此 Image 的垂直解析度(以“畫素/英寸”為單位)。
 
(繼承自 Image。)
公共屬性 Width 獲取此 Image 的寬度(以畫素為單位)。 (繼承自 Image。)
頁首 備註

點陣圖由圖形影象及其特性的畫素資料組成。 可使用許多標準格式將點陣圖儲存到檔案中。 GDI+ 支援下列檔案格式:BMP、GIF、EXIF、JPG、PNG 和 TIFF。 有關支援的格式的更多資訊,請參見點陣圖型別

可以使用  建構函式中的一種來從檔案、流和其他源建立影象,然後使用 Save 方法將這些影象儲存到流或檔案系統中。  

注意 注意

不能跨應用程式域訪問 Bitmap 類。 例如,如果您建立了一個動態 ,並在該域中建立了幾個畫筆、鋼筆和點陣圖,然後將這些物件傳遞迴主應用程式域,則您可以成功使用這些鋼筆和畫筆。 但是,如果您呼叫  方法來繪製封送的 Bitmap,您會收到以下異常資訊。

遠端處理無法在型別“System.Drawing.Image”上找到欄位“本機映像”。

示例

下面的程式碼示例演示瞭如何使用  和  方法從檔案構造新的 Bitmap,為影象重新著色。 

此示例旨在用於包含名為 Label1 的 Label、名為 PictureBox1 的  和名為 Button1 的  的 Windows 窗體。 將程式碼貼上到該窗體中,並將 Button1_Click 方法與按鈕的 Click 事件關聯。

Bitmap image1;

private void Button1_Click(System.Object sender, System.EventArgs e)
{

    try
    {
        // Retrieve the image.
        image1 = new Bitmap(@"C:\Documents and Settings\All Users\" 
            + @"Documents\My Music\music.bmp", true);

        int x, y;

        // Loop through the images pixels to reset color.
        for(x=0; x<image1.Width; x++)
        {
            for(y=0; y<image1.Height; y++)
            {
                Color pixelColor = image1.GetPixel(x, y);
                Color newColor = Color.FromArgb(pixelColor.R, 0, 0);
                image1.SetPixel(x, y, newColor);
            }
        }

        // Set the PictureBox to display the image.
        PictureBox1.Image = image1;

        // Display the pixel format in Label1.
        Label1.Text = "Pixel format: "+image1.PixelFormat.ToString();

    }
    catch(ArgumentException)
    {
        MessageBox.Show("There was an error." +
            "Check the path to the image file.");
    }
}