Bitmap コンストラクター

定義

Bitmap クラスの新しいインスタンスを初期化します。

オーバーロード

Bitmap(Image)

指定した既存のイメージを使用して、Bitmap クラスの新しいインスタンスを初期化します。

Bitmap(Stream)

指定したデータ ストリームで Bitmap クラスの新しいインスタンスを初期化します。

Bitmap(String)

指定したファイルで Bitmap クラスの新しいインスタンスを初期化します。

Bitmap(Image, Size)

指定したサイズにスケールを設定して、指定した既存のイメージで Bitmap クラスの新しいインスタンスを初期化します。

Bitmap(Int32, Int32)

指定したサイズを使用して、Bitmap クラスの新しいインスタンスを初期化します。

Bitmap(Stream, Boolean)

指定したデータ ストリームで Bitmap クラスの新しいインスタンスを初期化します。

Bitmap(String, Boolean)

指定したファイルで Bitmap クラスの新しいインスタンスを初期化します。

Bitmap(Type, String)

指定したリソースで Bitmap クラスの新しいインスタンスを初期化します。

Bitmap(Image, Int32, Int32)

指定したサイズにスケールを設定して、指定した既存のイメージで Bitmap クラスの新しいインスタンスを初期化します。

Bitmap(Int32, Int32, Graphics)

指定したサイズと指定した Graphics オブジェクトの解像度を使用して、Bitmap クラスの新しいインスタンスを初期化します。

Bitmap(Int32, Int32, PixelFormat)

指定したサイズと形式を使用して、Bitmap クラスの新しいインスタンスを初期化します。

Bitmap(Int32, Int32, Int32, PixelFormat, IntPtr)

指定したサイズ、ピクセル形式、ピクセル データを使用して、Bitmap クラスの新しいインスタンスを初期化します。

Bitmap(Image)

ソース:
Bitmap.cs
ソース:
Bitmap.cs
ソース:
Bitmap.cs

指定した既存のイメージを使用して、Bitmap クラスの新しいインスタンスを初期化します。

public:
 Bitmap(System::Drawing::Image ^ original);
public Bitmap (System.Drawing.Image original);
new System.Drawing.Bitmap : System.Drawing.Image -> System.Drawing.Bitmap
Public Sub New (original As Image)

パラメーター

original
Image

新しい Bitmap の作成元の Image

適用対象

Bitmap(Stream)

ソース:
Bitmap.cs
ソース:
Bitmap.cs
ソース:
Bitmap.cs

指定したデータ ストリームで Bitmap クラスの新しいインスタンスを初期化します。

public:
 Bitmap(System::IO::Stream ^ stream);
public Bitmap (System.IO.Stream stream);
new System.Drawing.Bitmap : System.IO.Stream -> System.Drawing.Bitmap
Public Sub New (stream As Stream)

パラメーター

stream
Stream

イメージの読み込みに使用するデータ ストリーム。

例外

stream にイメージ データが含まれていないか null です。

- または -

stream に、65,535 ピクセルよりも大きい 1 次元の PNG イメージ ファイルが含まれています。

次のコード例は、ストリームからビットマップを読み込む方法を示しています。

この例は、Windows フォームで使用するように設計されています。 という名前PictureBox1の を含むフォームをPictureBox作成します。 コードをフォームに貼り付け、フォームのコンストラクターまたはLoadイベント処理メソッドから メソッドを呼び出InitializeStreamBitmapします。

void InitializeStreamBitmap()
{
   try
   {
      System::Net::WebRequest^ request = System::Net::WebRequest::Create( "http://www.microsoft.com//h/en-us/r/ms_masthead_ltr.gif" );
      System::Net::WebResponse^ response = request->GetResponse();
      System::IO::Stream^ responseStream = response->GetResponseStream();
      Bitmap^ bitmap2 = gcnew Bitmap( responseStream );
      PictureBox1->Image = bitmap2;
   }
   catch ( System::Net::WebException^ ) 
   {
      MessageBox::Show( "There was an error opening the image file."
      "Check the URL" );
   }

}
private void InitializeStreamBitmap()
{
    try
    {
        System.Net.WebRequest request = 
            System.Net.WebRequest.Create(
            "http://www.microsoft.com//h/en-us/r/ms_masthead_ltr.gif");
        System.Net.WebResponse response = request.GetResponse();
        System.IO.Stream responseStream = 
            response.GetResponseStream();
        Bitmap bitmap2 = new Bitmap(responseStream);
        PictureBox1.Image = bitmap2;
    }
    catch(System.Net.WebException)
    {
        MessageBox.Show("There was an error opening the image file."
           + "Check the URL");
    }
}
Private Sub InitializeStreamBitmap()
    Try
        Dim request As System.Net.WebRequest = _
            System.Net.WebRequest.Create( _
            "http://www.microsoft.com//h/en-us/r/ms_masthead_ltr.gif")
        Dim response As System.Net.WebResponse = request.GetResponse()
        Dim responseStream As System.IO.Stream = response.GetResponseStream()
        Dim bitmap2 As New Bitmap(responseStream)
        PictureBox1.Image = bitmap2

    Catch ex As System.Net.WebException
        MessageBox.Show("There was an error opening the image file. Check the URL")
    End Try
End Sub

注釈

の有効期間は、ストリームを開いたままにする Bitmap必要があります。

GDI+ デコーダーの制限により、 System.ArgumentException 65,535 ピクセルを超える単一次元の.pngイメージ ファイルからビットマップを構築すると、 がスローされます。

こちらもご覧ください

適用対象

Bitmap(String)

ソース:
Bitmap.cs
ソース:
Bitmap.cs
ソース:
Bitmap.cs

指定したファイルで Bitmap クラスの新しいインスタンスを初期化します。

public:
 Bitmap(System::String ^ filename);
public Bitmap (string filename);
new System.Drawing.Bitmap : string -> System.Drawing.Bitmap
Public Sub New (filename As String)

パラメーター

filename
String

ビットマップ ファイルの名前とパス。

例外

指定されたファイルが見つかりません。

注釈

ファイル名とパスは、アプリケーションに対する相対パスまたは絶対パスにすることができます。 BMP、GIF、EXIF、JPG、PNG、TIFF のファイル形式で画像を開くには、このコンストラクターを使用します。 サポートされている形式の詳細については、「 ビットマップの種類」を参照してください。 ファイルは、 が破棄されるまで Bitmap ロックされたままになります。

こちらもご覧ください

適用対象

Bitmap(Image, Size)

ソース:
Bitmap.cs
ソース:
Bitmap.cs
ソース:
Bitmap.cs

指定したサイズにスケールを設定して、指定した既存のイメージで Bitmap クラスの新しいインスタンスを初期化します。

public:
 Bitmap(System::Drawing::Image ^ original, System::Drawing::Size newSize);
public Bitmap (System.Drawing.Image original, System.Drawing.Size newSize);
new System.Drawing.Bitmap : System.Drawing.Image * System.Drawing.Size -> System.Drawing.Bitmap
Public Sub New (original As Image, newSize As Size)

パラメーター

original
Image

新しい Bitmap の作成元の Image

newSize
Size

新しい Bitmap のサイズを表す Size 構造。

例外

操作が失敗しました。

適用対象

Bitmap(Int32, Int32)

ソース:
Bitmap.cs
ソース:
Bitmap.cs
ソース:
Bitmap.cs

指定したサイズを使用して、Bitmap クラスの新しいインスタンスを初期化します。

public:
 Bitmap(int width, int height);
public Bitmap (int width, int height);
new System.Drawing.Bitmap : int * int -> System.Drawing.Bitmap
Public Sub New (width As Integer, height As Integer)

パラメーター

width
Int32

新しい Bitmap の幅 (ピクセル単位)。

height
Int32

新しい Bitmap の高さ (ピクセル単位)。

例外

操作が失敗しました。

注釈

このコンストラクターは、 の列挙値Format32bppArgbを使用して PixelFormatBitmap作成します。

適用対象

Bitmap(Stream, Boolean)

ソース:
Bitmap.cs
ソース:
Bitmap.cs
ソース:
Bitmap.cs

指定したデータ ストリームで Bitmap クラスの新しいインスタンスを初期化します。

public:
 Bitmap(System::IO::Stream ^ stream, bool useIcm);
public Bitmap (System.IO.Stream stream, bool useIcm);
new System.Drawing.Bitmap : System.IO.Stream * bool -> System.Drawing.Bitmap
Public Sub New (stream As Stream, useIcm As Boolean)

パラメーター

stream
Stream

イメージの読み込みに使用するデータ ストリーム。

useIcm
Boolean

この Bitmap に対して色補正を使用する場合は true、それ以外の場合は false です。

例外

stream にイメージ データが含まれていないか null です。

- または -

stream に、65,535 ピクセルよりも大きい 1 次元の PNG イメージ ファイルが含まれています。

注釈

の有効期間は、ストリームを開いたままにする Bitmap必要があります。

GDI+ デコーダーの制限により、 System.ArgumentException 65,535 ピクセルを超える単一次元の.pngイメージ ファイルからビットマップを構築すると、 がスローされます。

こちらもご覧ください

適用対象

Bitmap(String, Boolean)

ソース:
Bitmap.cs
ソース:
Bitmap.cs
ソース:
Bitmap.cs

指定したファイルで Bitmap クラスの新しいインスタンスを初期化します。

public:
 Bitmap(System::String ^ filename, bool useIcm);
public Bitmap (string filename, bool useIcm);
new System.Drawing.Bitmap : string * bool -> System.Drawing.Bitmap
Public Sub New (filename As String, useIcm As Boolean)

パラメーター

filename
String

ビットマップ ファイルの名前。

useIcm
Boolean

この Bitmap に対して色補正を使用する場合は true、それ以外の場合は false です。

次のコード例は、ファイルから新しいビットマップを構築する方法を示しています。 この例では、 GetPixel メソッドと SetPixel メソッドを使用してイメージの色を変更します。 また、 プロパティも使用します PixelFormat

この例は、それぞれ 、 と という名前Button1Label1PictureBox1の を含む LabelPictureBoxButton Windows フォームで使用するように設計されています。 コードをフォームに貼り付け、 メソッドを Button1_Click ボタンの Click イベントに関連付けます。

private:
   Bitmap^ image1;
   void Button1_Click( System::Object^ /*sender*/, System::EventArgs^ /*e*/ )
   {
      try
      {
         
         // Retrieve the image.
         image1 = gcnew Bitmap( "C:\\Documents and Settings\\All Users\\"
         "Documents\\My Music\\music.bmp",true );
         int x;
         int 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 = String::Format( "Pixel format: {0}", image1->PixelFormat );
      }
      catch ( ArgumentException^ ) 
      {
         MessageBox::Show( "There was an error."
         "Check the path to the image file." );
      }

   }
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.");
    }
}
Dim image1 As Bitmap

Private Sub Button1_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles Button1.Click

    Try
        ' Retrieve the image.
        image1 = New Bitmap( _
            "C:\Documents and Settings\All Users\Documents\My Music\music.bmp", _
            True)

        Dim x, y As Integer

        ' Loop through the images pixels to reset color.
        For x = 0 To image1.Width - 1
            For y = 0 To image1.Height - 1
                Dim pixelColor As Color = image1.GetPixel(x, y)
                Dim newColor As Color = _
                    Color.FromArgb(pixelColor.R, 0, 0)
                image1.SetPixel(x, y, newColor)
            Next
        Next

        ' Set the PictureBox to display the image.
        PictureBox1.Image = image1

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

    Catch ex As ArgumentException
        MessageBox.Show("There was an error." _
            & "Check the path to the image file.")
    End Try
End Sub

注釈

BMP、GIF、EXIF、JPG、PNG、TIFF のファイル形式で画像を開くには、このコンストラクターを使用します。 サポートされている形式の詳細については、「 ビットマップの種類」を参照してください。 ファイルは、 が破棄されるまで Bitmap ロックされたままになります。

こちらもご覧ください

適用対象

Bitmap(Type, String)

ソース:
Bitmap.cs
ソース:
Bitmap.cs
ソース:
Bitmap.cs

指定したリソースで Bitmap クラスの新しいインスタンスを初期化します。

public:
 Bitmap(Type ^ type, System::String ^ resource);
public Bitmap (Type type, string resource);
new System.Drawing.Bitmap : Type * string -> System.Drawing.Bitmap
Public Sub New (type As Type, resource As String)

パラメーター

type
Type

リソースの抽出に使用されるクラス。

resource
String

リソースの名前。

次のコード例では、型からビットマップを構築する方法と、 メソッドを使用する方法を Save 示します。 この例を実行するには、コードを Windows フォームに貼り付けます。 フォーム Paint のイベントを処理し、 メソッドを ConstructFromResourceSaveAsGif 呼び出し、 として渡します ePaintEventArgs

private:
    void ConstructFromResourceSaveAsGif(PaintEventArgs^ e)
    {
        // Construct a bitmap from the button image resource.
        Bitmap^ bmp1 = gcnew Bitmap(Button::typeid, "Button.bmp");
        String^ savePath =  
            Environment::GetEnvironmentVariable("TEMP") + "\\Button.bmp";

        try
        {
            // Save the image as a GIF.
            bmp1->Save(savePath, System::Drawing::Imaging::ImageFormat::Gif);
        }
        catch (IOException^)
        {
            // Carry on regardless
        }

        // Construct a new image from the GIF file.
        Bitmap^ bmp2 = nullptr;
        if (File::Exists(savePath))
        {
            bmp2 = gcnew Bitmap(savePath);
        }

        // Draw the two images.
        e->Graphics->DrawImage(bmp1, Point(10, 10));

        // If bmp1 did not save to disk, bmp2 may be null
        if (bmp2 != nullptr)
        {
            e->Graphics->DrawImage(bmp2, Point(10, 40));
        }

        // Dispose of the image files.
        delete bmp1;
        if (bmp2 != nullptr)
        {
            delete bmp2;
        }
    }
private void ConstructFromResourceSaveAsGif(PaintEventArgs e)
{

    // Construct a bitmap from the button image resource.
    Bitmap bmp1 = new Bitmap(typeof(Button), "Button.bmp");

    // Save the image as a GIF.
    bmp1.Save("c:\\button.gif", System.Drawing.Imaging.ImageFormat.Gif);

    // Construct a new image from the GIF file.
    Bitmap bmp2 = new Bitmap("c:\\button.gif");

    // Draw the two images.
    e.Graphics.DrawImage(bmp1, new Point(10, 10));
    e.Graphics.DrawImage(bmp2, new Point(10, 40));

    // Dispose of the image files.
    bmp1.Dispose();
    bmp2.Dispose();
}
Private Sub ConstructFromResourceSaveAsGif(ByVal e As PaintEventArgs)

    ' Construct a bitmap from the button image resource.
    Dim bmp1 As New Bitmap(GetType(Button), "Button.bmp")

    ' Save the image as a GIF.
    bmp1.Save("c:\button.gif", System.Drawing.Imaging.ImageFormat.Gif)

    ' Construct a new image from the GIF file.
    Dim bmp2 As New Bitmap("c:\button.gif")

    ' Draw the two images.
    e.Graphics.DrawImage(bmp1, New Point(10, 10))
    e.Graphics.DrawImage(bmp2, New Point(10, 40))

    ' Dispose of the image files.
    bmp1.Dispose()
    bmp2.Dispose()
End Sub

注釈

このコンストラクターは、指定された型の名前空間とリソースの文字列名を組み合わせて、アセンブリ マニフェストで一致するものを探します。 たとえば、 型と Button.bmpButtonこのコンストラクターに渡すと、 という名前System.Windows.Forms.Button.bmpのリソースが検索されます。

こちらもご覧ください

適用対象

Bitmap(Image, Int32, Int32)

ソース:
Bitmap.cs
ソース:
Bitmap.cs
ソース:
Bitmap.cs

指定したサイズにスケールを設定して、指定した既存のイメージで Bitmap クラスの新しいインスタンスを初期化します。

public:
 Bitmap(System::Drawing::Image ^ original, int width, int height);
public Bitmap (System.Drawing.Image original, int width, int height);
new System.Drawing.Bitmap : System.Drawing.Image * int * int -> System.Drawing.Bitmap
Public Sub New (original As Image, width As Integer, height As Integer)

パラメーター

original
Image

新しい Bitmap の作成元の Image

width
Int32

新しい Bitmap の幅 (ピクセル単位)。

height
Int32

新しい Bitmap の高さ (ピクセル単位)。

例外

操作が失敗しました。

適用対象

Bitmap(Int32, Int32, Graphics)

ソース:
Bitmap.cs
ソース:
Bitmap.cs
ソース:
Bitmap.cs

指定したサイズと指定した Graphics オブジェクトの解像度を使用して、Bitmap クラスの新しいインスタンスを初期化します。

public:
 Bitmap(int width, int height, System::Drawing::Graphics ^ g);
public Bitmap (int width, int height, System.Drawing.Graphics g);
new System.Drawing.Bitmap : int * int * System.Drawing.Graphics -> System.Drawing.Bitmap
Public Sub New (width As Integer, height As Integer, g As Graphics)

パラメーター

width
Int32

新しい Bitmap の幅 (ピクセル単位)。

height
Int32

新しい Bitmap の高さ (ピクセル単位)。

g
Graphics

新しい Bitmap の解像度を指定する Graphics オブジェクト。

例外

gnullです。

注釈

このメソッドが作成する新しい Bitmap は、 の プロパティと プロパティから DpiX 水平方向と DpiY 垂直方向の g解像度をそれぞれ受け取ります。

適用対象

Bitmap(Int32, Int32, PixelFormat)

ソース:
Bitmap.cs
ソース:
Bitmap.cs
ソース:
Bitmap.cs

指定したサイズと形式を使用して、Bitmap クラスの新しいインスタンスを初期化します。

public:
 Bitmap(int width, int height, System::Drawing::Imaging::PixelFormat format);
public Bitmap (int width, int height, System.Drawing.Imaging.PixelFormat format);
new System.Drawing.Bitmap : int * int * System.Drawing.Imaging.PixelFormat -> System.Drawing.Bitmap
Public Sub New (width As Integer, height As Integer, format As PixelFormat)

パラメーター

width
Int32

新しい Bitmap の幅 (ピクセル単位)。

height
Int32

新しい Bitmap の高さ (ピクセル単位)。

format
PixelFormat

新しい Bitmap のピクセル形式。 これは、 で Format始まる値を指定する必要があります。

例外

名前が Format で始まらない PixelFormat 値が指定されます。 たとえば、Gdi を指定すると、ArgumentException が発生しますが、Format48bppRgb を指定した場合は発生しません。

適用対象

Bitmap(Int32, Int32, Int32, PixelFormat, IntPtr)

ソース:
Bitmap.cs
ソース:
Bitmap.cs
ソース:
Bitmap.cs

指定したサイズ、ピクセル形式、ピクセル データを使用して、Bitmap クラスの新しいインスタンスを初期化します。

public:
 Bitmap(int width, int height, int stride, System::Drawing::Imaging::PixelFormat format, IntPtr scan0);
public Bitmap (int width, int height, int stride, System.Drawing.Imaging.PixelFormat format, IntPtr scan0);
new System.Drawing.Bitmap : int * int * int * System.Drawing.Imaging.PixelFormat * nativeint -> System.Drawing.Bitmap
Public Sub New (width As Integer, height As Integer, stride As Integer, format As PixelFormat, scan0 As IntPtr)

パラメーター

width
Int32

新しい Bitmap の幅 (ピクセル単位)。

height
Int32

新しい Bitmap の高さ (ピクセル単位)。

stride
Int32

スキャン ラインの間のバイト オフセット数を指定する整数。 これには、通常 (必須ではありません)、ピクセルあたりのバイト数という形式 (16 ビット/ピクセルの場合は 2) にビットマップの幅を乗じた値を指定します。 このパラメーターに渡す値は、4 の倍数である必要があります。

format
PixelFormat

新しい Bitmap のピクセル形式。 これは、 で Format始まる値を指定する必要があります。

scan0
IntPtr

nativeint

ピクセル データを格納するバイトの配列へのポインター。

例外

名前が Format で始まらない PixelFormat 値が指定されます。 たとえば、Gdi を指定すると、ArgumentException が発生しますが、Format48bppRgb を指定した場合は発生しません。

次のコード例は、 コンストラクターの使用方法を Bitmap(Int32, Int32, Int32, PixelFormat, IntPtr) 示しています。 この例は、Windows フォームで使用するように設計されており、イベントのパラメーターである パラメーターがPaint必要PaintEventArgsです。

private void BitmapConstructorEx(PaintEventArgs e)
{

    // Create a bitmap.
    Bitmap bmp = new Bitmap("c:\\fakePhoto.jpg");
    
   // Retrieve the bitmap data from the bitmap.
    System.Drawing.Imaging.BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), 
        ImageLockMode.ReadOnly, bmp.PixelFormat);

    //Create a new bitmap.
    Bitmap newBitmap = new Bitmap(200, 200, bmpData.Stride, bmp.PixelFormat, bmpData.Scan0);

    bmp.UnlockBits(bmpData);

    // Draw the new bitmap.
    e.Graphics.DrawImage(newBitmap, 10, 10);
}
Private Sub BitmapConstructorEx(ByVal e As PaintEventArgs)

    ' Create a bitmap.
    Dim bmp As New Bitmap("c:\fakePhoto.jpg")

    ' Retrieve the bitmap data from the bitmap.
    Dim bmpData As System.Drawing.Imaging.BitmapData = bmp.LockBits(New Rectangle(0, 0, bmp.Width, bmp.Height), _
        ImageLockMode.ReadOnly, bmp.PixelFormat)

    'Create a new bitmap.
    Dim newBitmap As New Bitmap(200, 200, bmpData.Stride, bmp.PixelFormat, bmpData.Scan0)

    bmp.UnlockBits(bmpData)

    ' Draw the new bitmap.
    e.Graphics.DrawImage(newBitmap, 10, 10)

End Sub

注釈

呼び出し元は、 パラメーターで指定されたメモリ ブロックの割り当てと解放を scan0 行います。 ただし、関連 Bitmap する が解放されるまで、メモリを解放しないでください。

適用対象