Graphics.DrawImage 方法

定義

在指定的位置和原始大小繪製指定的 Image

多載

DrawImage(Image, Single, Single, RectangleF, GraphicsUnit)

在指定的位置繪製影像的一部分。

DrawImage(Image, Effect, RectangleF, Matrix, GraphicsUnit, ImageAttributes)
DrawImage(Image, Point[], Rectangle, GraphicsUnit, ImageAttributes, Graphics+DrawImageAbort)

在指定的位置和具有指定大小的指定位置繪製指定 Image 的指定部分。

DrawImage(Image, PointF[], RectangleF, GraphicsUnit, ImageAttributes, Graphics+DrawImageAbort)

在指定的位置和具有指定大小的指定位置繪製指定 Image 的指定部分。

DrawImage(Image, Point[], Rectangle, GraphicsUnit, ImageAttributes, Graphics+DrawImageAbort, Int32)

在指定的位置和具有指定大小的指定位置繪製指定 Image 的指定部分。

DrawImage(Image, PointF[], RectangleF, GraphicsUnit, ImageAttributes, Graphics+DrawImageAbort, Int32)

在指定的位置和具有指定大小的指定位置繪製指定 Image 的指定部分。

DrawImage(Image, Rectangle, Int32, Int32, Int32, Int32, GraphicsUnit)

在指定的位置和具有指定大小的指定位置繪製指定 Image 的指定部分。

DrawImage(Image, Rectangle, Int32, Int32, Int32, Int32, GraphicsUnit, ImageAttributes)

在指定的位置和具有指定大小的指定位置繪製指定 Image 的指定部分。

DrawImage(Image, Rectangle, Single, Single, Single, Single, GraphicsUnit, ImageAttributes)

在指定的位置和具有指定大小的指定位置繪製指定 Image 的指定部分。

DrawImage(Image, Rectangle, Int32, Int32, Int32, Int32, GraphicsUnit, ImageAttributes, Graphics+DrawImageAbort)

在指定的位置和具有指定大小的指定位置繪製指定 Image 的指定部分。

DrawImage(Image, Rectangle, Single, Single, Single, Single, GraphicsUnit, ImageAttributes, Graphics+DrawImageAbort)

在指定的位置和具有指定大小的指定位置繪製指定 Image 的指定部分。

DrawImage(Image, Rectangle, Int32, Int32, Int32, Int32, GraphicsUnit, ImageAttributes, Graphics+DrawImageAbort, IntPtr)

在指定的位置和具有指定大小的指定位置繪製指定 Image 的指定部分。

DrawImage(Image, Rectangle, Single, Single, Single, Single, GraphicsUnit, ImageAttributes, Graphics+DrawImageAbort, IntPtr)

在指定的位置和具有指定大小的指定位置繪製指定 Image 的指定部分。

DrawImage(Image, Rectangle, Single, Single, Single, Single, GraphicsUnit)

在指定的位置和具有指定大小的指定位置繪製指定 Image 的指定部分。

DrawImage(Image, Int32, Int32, Int32, Int32)

在指定的位置繪製指定的 Image,並具有指定的大小。

DrawImage(Image, Single, Single, Single, Single)

在指定的位置繪製指定的 Image,並具有指定的大小。

DrawImage(Image, PointF[], RectangleF, GraphicsUnit, ImageAttributes)

在指定的位置和具有指定大小的指定位置繪製指定 Image 的指定部分。

DrawImage(Image, Effect)
DrawImage(Image, Point)

使用指定位置的原始實體大小,繪製指定的 Image

DrawImage(Image, Point[])

在指定的位置繪製指定的 Image,並具有指定的圖案和大小。

DrawImage(Image, PointF)

使用指定位置的原始實體大小,繪製指定的 Image

DrawImage(Image, PointF[])

在指定的位置繪製指定的 Image,並具有指定的圖案和大小。

DrawImage(Image, Rectangle)

在指定的位置繪製指定的 Image,並具有指定的大小。

DrawImage(Image, RectangleF)

在指定的位置繪製指定的 Image,並具有指定的大小。

DrawImage(Image, Int32, Int32, Rectangle, GraphicsUnit)

在指定的位置繪製影像的一部分。

DrawImage(Image, Single, Single)

使用指定位置的原始實體大小,繪製指定的 Image

DrawImage(Image, Point[], Rectangle, GraphicsUnit)

在指定的位置和具有指定大小的指定位置繪製指定 Image 的指定部分。

DrawImage(Image, PointF[], RectangleF, GraphicsUnit)

在指定的位置和具有指定大小的指定位置繪製指定 Image 的指定部分。

DrawImage(Image, Rectangle, Rectangle, GraphicsUnit)

在指定的位置和具有指定大小的指定位置繪製指定 Image 的指定部分。

DrawImage(Image, RectangleF, RectangleF, GraphicsUnit)

在指定的位置和具有指定大小的指定位置繪製指定 Image 的指定部分。

DrawImage(Image, Int32, Int32)

使用原始的實體大小,在座標組所指定的位置繪製指定的影像。

DrawImage(Image, Point[], Rectangle, GraphicsUnit, ImageAttributes)

在指定的位置繪製指定 Image 的指定部分。

DrawImage(Image, Single, Single, RectangleF, GraphicsUnit)

來源:
Graphics.cs
來源:
Graphics.cs
來源:
Graphics.cs
來源:
Graphics.cs
來源:
Graphics.cs

在指定的位置繪製影像的一部分。

C#
public void DrawImage (System.Drawing.Image image, float x, float y, System.Drawing.RectangleF srcRect, System.Drawing.GraphicsUnit srcUnit);

參數

image
Image

要繪製的 Image

x
Single

繪製影像左上角的 X 座標。

y
Single

繪製影像左上角的 Y 座標。

srcRect
RectangleF

RectangleF 結構,指定要繪製之 Image 的部分。

srcUnit
GraphicsUnit

GraphicsUnit 列舉的成員,指定 srcRect 參數所使用的量值單位。

例外狀況

image null

範例

下列程式代碼範例是專為搭配 Windows Forms 使用而設計,而且需要 PaintEventArgse,這是 Paint 事件處理程式的參數。 程式代碼會執行下列動作:

  • 從範例資料夾中的 JPEG 檔案 SampImag.jpg 建立映像。

  • 建立要繪製影像左上角的座標。

  • 建立來源矩形,從中擷取影像的一部分。

  • 將來源矩形的測量單位設定為圖元。

  • 將影像繪製到畫面。

來源矩形的大小會決定未調整的原始影像繪製到畫面的哪個部分。

C#
public void DrawImage2FloatRectF(PaintEventArgs e)
{
             
    // Create image.
    Image newImage = Image.FromFile("SampImag.jpg");
             
    // Create coordinates for upper-left corner of image.
    float x = 100.0F;
    float y = 100.0F;
             
    // Create rectangle for source image.
    RectangleF srcRect = new RectangleF(50.0F, 50.0F, 150.0F, 150.0F);
    GraphicsUnit units = GraphicsUnit.Pixel;
             
    // Draw image to screen.
    e.Graphics.DrawImage(newImage, x, y, srcRect, units);
}

備註

Image 會儲存圖元寬度的值,以及水平解析度的值(每英吋點)。 以英吋為單位的影像實體寬度是像素寬度除以水平解析度。 例如,圖元寬度為 360 的影像,而水平解析度為每英吋 72 個點,其實體寬度為 5 英吋。 類似的備註適用於圖元高度和實體高度。

這個方法會使用其實體大小繪製影像的一部分,因此影像部分的大小會以英吋為單位,而不論顯示裝置的解析度(每英吋點數)為何。 例如,假設影像部分的圖元寬度為 216,而水平解析度為每英吋 72 個點。 如果您呼叫此方法,在解析度為每英吋 96 點的裝置上繪製該影像部分,轉譯影像部分的圖元寬度會是 (216/72)*96 = 288。

另請參閱

適用於

.NET 9 及其他版本
產品 版本
.NET 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

DrawImage(Image, Effect, RectangleF, Matrix, GraphicsUnit, ImageAttributes)

來源:
Graphics.cs
來源:
Graphics.cs
C#
public void DrawImage (System.Drawing.Image image, System.Drawing.Imaging.Effects.Effect effect, System.Drawing.RectangleF srcRect = default, System.Drawing.Drawing2D.Matrix? transform = default, System.Drawing.GraphicsUnit srcUnit = System.Drawing.GraphicsUnit.Pixel, System.Drawing.Imaging.ImageAttributes? imageAttr = default);

參數

image
Image
effect
Effect
srcRect
RectangleF
transform
Matrix
srcUnit
GraphicsUnit
imageAttr
ImageAttributes

適用於

.NET 9 和 Windows Desktop 9
產品 版本
.NET 9
Windows Desktop 9

DrawImage(Image, Point[], Rectangle, GraphicsUnit, ImageAttributes, Graphics+DrawImageAbort)

來源:
Graphics.cs
來源:
Graphics.cs
來源:
Graphics.cs
來源:
Graphics.cs
來源:
Graphics.cs

在指定的位置和具有指定大小的指定位置繪製指定 Image 的指定部分。

C#
public void DrawImage (System.Drawing.Image image, System.Drawing.Point[] destPoints, System.Drawing.Rectangle srcRect, System.Drawing.GraphicsUnit srcUnit, System.Drawing.Imaging.ImageAttributes? imageAttr, System.Drawing.Graphics.DrawImageAbort? callback);
C#
public void DrawImage (System.Drawing.Image image, System.Drawing.Point[] destPoints, System.Drawing.Rectangle srcRect, System.Drawing.GraphicsUnit srcUnit, System.Drawing.Imaging.ImageAttributes imageAttr, System.Drawing.Graphics.DrawImageAbort callback);

參數

image
Image

要繪製的 Image

destPoints
Point[]

定義平行投影之三個 PointF 結構的陣列。

srcRect
Rectangle

Rectangle 結構,指定要繪製之 image 物件的部分。

srcUnit
GraphicsUnit

GraphicsUnit 列舉的成員,指定 srcRect 參數所使用的量值單位。

imageAttr
ImageAttributes

ImageAttributes,指定 image 物件的重新著色和伽瑪資訊。

callback
Graphics.DrawImageAbort

Graphics.DrawImageAbort 委派,指定要在影像繪製期間呼叫的方法。 這個方法經常呼叫,以檢查是否要根據應用程式判斷的準則停止執行 DrawImage(Image, Point[], Rectangle, GraphicsUnit, ImageAttributes, Graphics+DrawImageAbort) 方法。

例外狀況

image null

範例

下列程式代碼範例是專為搭配 Windows Forms 使用而設計,而且需要 PaintEventArgse,這是 Paint 事件處理程式的參數。 程序代碼會先定義 Graphics.DrawImageAbort 委派的回呼方法;定義簡單明瞭,只是測試 DrawImage 方法是否以 null callBackData 參數呼叫它。 此範例的主體會執行下列動作:

  • 建立 Graphics.DrawImageAbort 回呼方法的實例:

  • 從範例資料夾中的 JPEG 檔案 SampImag.jpg 建立映像。

  • 建立點,以定義用來繪製影像的平行投影。

  • 建立矩形以選取要繪製的影像部分。

  • 將圖形繪圖單位設定為圖元。

  • 將原始影像繪製到畫面。

  • 建立額外的平行投影,在其中繪製調整后的影像。

  • 建立並設定調整影像的屬性,以具有大於一般伽瑪值。

  • 將調整后的影像繪製到畫面。

針對原始、未調整的平行投影,位置會在畫面上找出影像,而矩形的大小以及平行投影的大小和形狀會決定繪製影像的縮放和剪下。

由於此範例使用多載未傳遞 callBackData 參數,因此 Graphics.DrawImageAbort 回呼會傳回 true,這會導致 DrawImage 方法結束,而範例中包含的例外狀況處理程式代碼會列印出例外狀況文字,而不是繪製影像。

C#

// Define DrawImageAbort callback method.
private bool DrawImageCallback1(IntPtr callBackData)
{
             
    // Test for call that passes callBackData parameter.
    if(callBackData==IntPtr.Zero)
    {
             
        // If no callBackData passed, abort DrawImage method.
        return true;
    }
    else
    {
             
        // If callBackData passed, continue DrawImage method.
        return false;
    }
}
private void DrawImageParaRectAttribAbort(PaintEventArgs e)
{
             
    // Create callback method.
    Graphics.DrawImageAbort imageCallback
        = new Graphics.DrawImageAbort(DrawImageCallback1);
             
    // Create image.
    Image newImage = Image.FromFile("SampImag.jpg");
             
    // Create parallelogram for drawing original image.
    Point ulCorner = new Point(100, 100);
    Point urCorner = new Point(550, 100);
    Point llCorner = new Point(150, 250);
    Point[] destPara1 = {ulCorner, urCorner, llCorner};
             
    // Create rectangle for source image.
    Rectangle srcRect = new Rectangle(50, 50, 150, 150);
    GraphicsUnit units = GraphicsUnit.Pixel;
             
    // Draw original image to screen.
    e.Graphics.DrawImage(newImage, destPara1, srcRect, units);
             
    // Create parallelogram for drawing adjusted image.
    Point ulCorner2 = new Point(325, 100);
    Point urCorner2 = new Point(550, 100);
    Point llCorner2 = new Point(375, 250);
    Point[] destPara2 = {ulCorner2, urCorner2, llCorner2};
             
    // Create image attributes and set large gamma.
    ImageAttributes imageAttr = new ImageAttributes();
    imageAttr.SetGamma(4.0F);
    try
    {
        checked
        {
             
            // Draw image to screen.
            e.Graphics.DrawImage(
                newImage,
                destPara2,
                srcRect,
                units,
                imageAttr,
                imageCallback);
        }
    }
    catch (Exception ex)
    {
        e.Graphics.DrawString(
            ex.ToString(),
            new Font("Arial", 8),
            Brushes.Black,
            new PointF(0, 0));
    }
}

備註

destPoints 參數會指定平行投影的三點。 三個 PointF 結構代表平行投影的左上角、右上角和左下角。 第四個點是從前三個推斷出來的,以形成平行投影。

srcRect 參數會指定要繪製之 image 物件的矩形部分。 此部分會縮放並剪下,以符合 destPoints 參數所指定的平行投影。

具有 callback 參數的這個多載提供一旦根據應用程式所決定準則啟動影像時停止繪製的方法。 例如,應用程式可以開始繪製大型影像,而使用者可能會將影像捲動到畫面上,在此情況下,應用程式可能會停止繪圖。

另請參閱

適用於

.NET 9 及其他版本
產品 版本
.NET 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

DrawImage(Image, PointF[], RectangleF, GraphicsUnit, ImageAttributes, Graphics+DrawImageAbort)

來源:
Graphics.cs
來源:
Graphics.cs
來源:
Graphics.cs
來源:
Graphics.cs
來源:
Graphics.cs

在指定的位置和具有指定大小的指定位置繪製指定 Image 的指定部分。

C#
public void DrawImage (System.Drawing.Image image, System.Drawing.PointF[] destPoints, System.Drawing.RectangleF srcRect, System.Drawing.GraphicsUnit srcUnit, System.Drawing.Imaging.ImageAttributes? imageAttr, System.Drawing.Graphics.DrawImageAbort? callback);
C#
public void DrawImage (System.Drawing.Image image, System.Drawing.PointF[] destPoints, System.Drawing.RectangleF srcRect, System.Drawing.GraphicsUnit srcUnit, System.Drawing.Imaging.ImageAttributes imageAttr, System.Drawing.Graphics.DrawImageAbort callback);

參數

image
Image

要繪製的 Image

destPoints
PointF[]

定義平行投影之三個 PointF 結構的陣列。

srcRect
RectangleF

RectangleF 結構,指定要繪製之 image 物件的部分。

srcUnit
GraphicsUnit

GraphicsUnit 列舉的成員,指定 srcRect 參數所使用的量值單位。

imageAttr
ImageAttributes

ImageAttributes,指定 image 物件的重新著色和伽瑪資訊。

callback
Graphics.DrawImageAbort

Graphics.DrawImageAbort 委派,指定要在影像繪製期間呼叫的方法。 這個方法經常呼叫,以檢查是否要根據應用程式判斷的準則停止執行 DrawImage(Image, PointF[], RectangleF, GraphicsUnit, ImageAttributes, Graphics+DrawImageAbort) 方法。

例外狀況

image null

範例

下列程式代碼範例是專為搭配 Windows Forms 使用而設計,而且需要 PaintEventArgse,這是 Paint 事件處理程式的參數。 程序代碼會先定義 Graphics.DrawImageAbort 委派的回呼方法;定義簡單明瞭,只是測試 DrawImage 方法是否以 null callBackData 參數呼叫它。 此範例的主體會執行下列動作:

  • 建立 Graphics.DrawImageAbort 回呼方法的實例。

  • 從範例資料夾中的 JPEG 檔案 SampImag.jpg 建立映像。

  • 建立點,以定義用來繪製影像的平行投影。

  • 建立矩形以選取要繪製的影像部分。

  • 將圖形繪圖單位設定為圖元。

  • 將原始影像繪製到畫面。

  • 建立額外的平行投影,在其中繪製調整后的影像。

  • 建立並設定調整影像的屬性,以具有大於一般伽瑪值。

  • 將調整后的影像繪製到畫面。

針對原始、未調整的平行投影,位置會在畫面上找出影像,而矩形的大小以及平行投影的大小和形狀會決定繪製影像的縮放和剪下。

由於此範例使用多載未傳遞 callBackData 參數,因此 Graphics.DrawImageAbort 回呼會傳回 true,這會導致 DrawImage 方法結束,而範例中包含的例外狀況處理程式代碼會列印出例外狀況文字,而不是繪製影像。

C#

// Define DrawImageAbort callback method.
private bool DrawImageCallback3(IntPtr callBackData)
{
             
    // Test for call that passes callBackData parameter.
    if(callBackData==IntPtr.Zero)
    {
             
        // If no callBackData passed, abort DrawImage method.
        return true;
    }
    else
    {
             
        // If callBackData passed, continue DrawImage method.
        return false;
    }
}
private void DrawImageParaFRectAttribAbort(PaintEventArgs e)
{
             
    // Create callback method.
    Graphics.DrawImageAbort imageCallback
        = new Graphics.DrawImageAbort(DrawImageCallback3);
             
    // Create image.
    Image newImage = Image.FromFile("SampImag.jpg");
             
    // Create parallelogram for drawing original image.
    PointF ulCorner1 = new PointF(100.0F, 100.0F);
    PointF urCorner1 = new PointF(325.0F, 100.0F);
    PointF llCorner1 = new PointF(150.0F, 250.0F);
    PointF[] destPara1 = {ulCorner1, urCorner1, llCorner1};
             
    // Create rectangle for source image.
    RectangleF srcRect = new RectangleF(50.0F, 50.0F, 150.0F, 150.0F);
    GraphicsUnit units = GraphicsUnit.Pixel;
             
    // Create parallelogram for drawing adjusted image.
    PointF ulCorner2 = new PointF(325.0F, 100.0F);
    PointF urCorner2 = new PointF(550.0F, 100.0F);
    PointF llCorner2 = new PointF(375.0F, 250.0F);
    PointF[] destPara2 = {ulCorner2, urCorner2, llCorner2};
             
    // Draw original image to screen.
    e.Graphics.DrawImage(newImage, destPara1, srcRect, units);
             
    // Create image attributes and set large gamma.
    ImageAttributes imageAttr = new ImageAttributes();
    imageAttr.SetGamma(4.0F);
    try
    {
        checked
        {
             
            // Draw adjusted image to screen.
            e.Graphics.DrawImage(
                newImage,
                destPara2,
                srcRect,
                units,
                imageAttr,
                imageCallback);
        }
    }
    catch (Exception ex)
    {
        e.Graphics.DrawString(
            ex.ToString(),
            new Font("Arial", 8),
            Brushes.Black,
            new PointF(0, 0));
    }
}

備註

destPoints 參數會指定平行投影的三點。 三個 PointF 結構代表平行投影的左上角、右上角和左下角。 第四個點是從前三個推斷出來的,以形成平行投影。

srcRect 參數會指定要繪製之 image 物件的矩形部分。 此部分會縮放並剪下,以符合 destPoints 參數所指定的平行投影。

具有 callback 參數的這個多載提供一旦根據應用程式所決定準則啟動影像時停止繪製的方法。 例如,應用程式可以開始繪製大型影像,而使用者可能會將影像捲動到畫面上,在此情況下,應用程式可能會停止繪圖。

另請參閱

適用於

.NET 9 及其他版本
產品 版本
.NET 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

DrawImage(Image, Point[], Rectangle, GraphicsUnit, ImageAttributes, Graphics+DrawImageAbort, Int32)

來源:
Graphics.cs
來源:
Graphics.cs
來源:
Graphics.cs
來源:
Graphics.cs
來源:
Graphics.cs

在指定的位置和具有指定大小的指定位置繪製指定 Image 的指定部分。

C#
public void DrawImage (System.Drawing.Image image, System.Drawing.Point[] destPoints, System.Drawing.Rectangle srcRect, System.Drawing.GraphicsUnit srcUnit, System.Drawing.Imaging.ImageAttributes? imageAttr, System.Drawing.Graphics.DrawImageAbort? callback, int callbackData);
C#
public void DrawImage (System.Drawing.Image image, System.Drawing.Point[] destPoints, System.Drawing.Rectangle srcRect, System.Drawing.GraphicsUnit srcUnit, System.Drawing.Imaging.ImageAttributes imageAttr, System.Drawing.Graphics.DrawImageAbort callback, int callbackData);

參數

image
Image

要繪製的 Image

destPoints
Point[]

定義平行投影之三個 PointF 結構的陣列。

srcRect
Rectangle

Rectangle 結構,指定要繪製之 image 物件的部分。

srcUnit
GraphicsUnit

GraphicsUnit 列舉的成員,指定 srcRect 參數所使用的量值單位。

imageAttr
ImageAttributes

ImageAttributes,指定 image 物件的重新著色和伽瑪資訊。

callback
Graphics.DrawImageAbort

Graphics.DrawImageAbort 委派,指定要在影像繪製期間呼叫的方法。 這個方法經常呼叫,以檢查是否要根據應用程式判斷的準則停止執行 DrawImage(Image, Point[], Rectangle, GraphicsUnit, ImageAttributes, Graphics+DrawImageAbort, Int32) 方法。

callbackData
Int32

值,指定檢查是否停止執行 DrawImage(Image, Point[], Rectangle, GraphicsUnit, ImageAttributes, Graphics+DrawImageAbort, Int32) 方法時要使用的 Graphics.DrawImageAbort 委派的其他數據。

範例

下列程式代碼範例是專為搭配 Windows Forms 使用而設計,而且需要 PaintEventArgse,這是 Paint 事件處理程式的參數。 程序代碼會先定義 Graphics.DrawImageAbort 委派的回呼方法;定義簡單明瞭,只是測試 DrawImage 方法是否以 null callBackData 參數呼叫它。 此範例的主體會執行下列動作:

  • 建立 Graphics.DrawImageAbort 回呼方法的實例。

  • 從範例資料夾中的 JPEG 檔案 SampImag.jpg 建立映像。

  • 建立點,以定義用來繪製影像的平行投影。

  • 建立矩形以選取要繪製的影像部分。

  • 將圖形繪圖單位設定為圖元。

  • 將原始影像繪製到畫面。

  • 建立額外的平行投影,在其中繪製調整后的影像。

  • 建立並設定調整影像的屬性,以具有大於一般伽瑪值。

  • 將調整后的影像繪製到畫面。

針對原始、未調整的平行投影,位置會在畫面上找出影像,而矩形的大小以及平行投影的大小和形狀會決定繪製影像的縮放和剪下。

因為這個範例會使用傳遞 callBackData 參數的多載,Graphics.DrawImageAbort 回呼會傳回 false,這會導致 DrawImage 方法繼續,而此範例會將調整后的影像繪製到畫面。

C#

// Define DrawImageAbort callback method.
private bool DrawImageCallback2(IntPtr callBackData)
{
             
    // Test for call that passes callBackData parameter.
    if(callBackData==IntPtr.Zero)
    {
             
        // If no callBackData passed, abort DrawImage method.
        return true;
    }
    else
    {
             
        // If callBackData passed, continue DrawImage method.
        return false;
    }
}
private void DrawImageParaRectAttribAbortData(PaintEventArgs e)
{
             
    // Create callback method.
    Graphics.DrawImageAbort imageCallback
        = new Graphics.DrawImageAbort(DrawImageCallback2);
    int imageCallbackData = 1;
             
    // Create image.
    Image newImage = Image.FromFile("SampImag.jpg");
             
    // Create parallelogram for drawing original image.
    Point ulCorner = new Point(100, 100);
    Point urCorner = new Point(550, 100);
    Point llCorner = new Point(150, 250);
    Point[] destPara1 = {ulCorner, urCorner, llCorner};
             
    // Create rectangle for source image.
    Rectangle srcRect = new Rectangle(50, 50, 150, 150);
    GraphicsUnit units = GraphicsUnit.Pixel;
             
    // Draw original image to screen.
    e.Graphics.DrawImage(newImage, destPara1, srcRect, units);
             
    // Create parallelogram for drawing adjusted image.
    Point ulCorner2 = new Point(325, 100);
    Point urCorner2 = new Point(550, 100);
    Point llCorner2 = new Point(375, 250);
    Point[] destPara2 = {ulCorner2, urCorner2, llCorner2};
             
    // Create image attributes and set large gamma.
    ImageAttributes imageAttr = new ImageAttributes();
    imageAttr.SetGamma(4.0F);
    try
    {
        checked
        {
             
            // Draw image to screen.
            e.Graphics.DrawImage(
                newImage,
                destPara2,
                srcRect,
                units,
                imageAttr,
                imageCallback,
                imageCallbackData);
        }
    }
    catch (Exception ex)
    {
        e.Graphics.DrawString(
            ex.ToString(),
            new Font("Arial", 8),
            Brushes.Black,
            new PointF(0, 0));
    }
}

備註

destPoints 參數會指定平行投影的三點。 三個 PointF 結構代表平行投影的左上角、右上角和左下角。 第四個點是從前三個推斷出來的,以形成平行投影。

srcRect 參數會指定要繪製之 image 物件的矩形部分。 此部分會縮放並剪下,以符合 destPoints 參數所指定的平行投影。

具有 callbackcallbackData 參數的這個多載會根據應用程式所決定的準則和數據,提供停止影像繪製的方法。 例如,應用程式可以開始繪製大型影像,而使用者可能會將影像捲動到畫面上,在此情況下,應用程式可能會停止繪圖。

另請參閱

適用於

.NET 9 及其他版本
產品 版本
.NET 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

DrawImage(Image, PointF[], RectangleF, GraphicsUnit, ImageAttributes, Graphics+DrawImageAbort, Int32)

來源:
Graphics.cs
來源:
Graphics.cs
來源:
Graphics.cs
來源:
Graphics.cs
來源:
Graphics.cs

在指定的位置和具有指定大小的指定位置繪製指定 Image 的指定部分。

C#
public void DrawImage (System.Drawing.Image image, System.Drawing.PointF[] destPoints, System.Drawing.RectangleF srcRect, System.Drawing.GraphicsUnit srcUnit, System.Drawing.Imaging.ImageAttributes? imageAttr, System.Drawing.Graphics.DrawImageAbort? callback, int callbackData);
C#
public void DrawImage (System.Drawing.Image image, System.Drawing.PointF[] destPoints, System.Drawing.RectangleF srcRect, System.Drawing.GraphicsUnit srcUnit, System.Drawing.Imaging.ImageAttributes imageAttr, System.Drawing.Graphics.DrawImageAbort callback, int callbackData);

參數

image
Image

要繪製的 Image

destPoints
PointF[]

定義平行投影之三個 PointF 結構的陣列。

srcRect
RectangleF

RectangleF 結構,指定要繪製之 image 物件的部分。

srcUnit
GraphicsUnit

GraphicsUnit 列舉的成員,指定 srcRect 參數所使用的量值單位。

imageAttr
ImageAttributes

ImageAttributes,指定 image 物件的重新著色和伽瑪資訊。

callback
Graphics.DrawImageAbort

Graphics.DrawImageAbort 委派,指定要在影像繪製期間呼叫的方法。 這個方法經常呼叫,以檢查是否要根據應用程式判斷的準則停止執行 DrawImage(Image, PointF[], RectangleF, GraphicsUnit, ImageAttributes, Graphics+DrawImageAbort, Int32) 方法。

callbackData
Int32

值,指定檢查是否停止執行 DrawImage(Image, PointF[], RectangleF, GraphicsUnit, ImageAttributes, Graphics+DrawImageAbort, Int32) 方法時要使用的 Graphics.DrawImageAbort 委派的其他數據。

例外狀況

image null

範例

下列程式代碼範例是專為搭配 Windows Forms 使用而設計,而且需要 PaintEventArgse,這是 PaPaint 事件處理程式的參數。 程序代碼會先定義 Graphics.DrawImageAbort 委派的回呼方法;定義簡單明瞭,只是測試 DrawImage 方法是否以 null callBackData 參數呼叫它。 此範例的主體會執行下列動作:

  • 建立 Graphics.DrawImageAbort 回呼方法的實例。

  • 從範例資料夾中的 JPEG 檔案 SampImag.jpg 建立映像。

  • 建立點,以定義用來繪製影像的平行投影。

  • 建立矩形以選取要繪製的影像部分。

  • 將圖形繪圖單位設定為圖元。

  • 將原始影像繪製到畫面。

  • 建立額外的平行投影,在其中繪製調整后的影像。

  • 建立並設定調整影像的屬性,以具有大於一般伽瑪值。

  • 將調整后的影像繪製到畫面。

針對原始、未調整的平行投影,位置會在畫面上找出影像,而矩形的大小以及平行投影的大小和形狀會決定繪製影像的縮放和剪下。

因為這個範例會使用傳遞 callBackData 參數的多載,Graphics.DrawImageAbort 回呼會傳回 false,這會導致 DrawImage 方法繼續,而此範例會將調整后的影像繪製到畫面。

C#

// Define DrawImageAbort callback method.
private bool DrawImageCallback4(IntPtr callBackData)
{
             
    // Test for call that passes callBackData parameter.
    if(callBackData==IntPtr.Zero)
    {
             
        // If no callBackData passed, abort DrawImage method.
        return true;
    }
    else
    {
             
        // If callBackData passed, continue DrawImage method.
        return false;
    }
}
private void DrawImageParaFRectAttribAbortData(PaintEventArgs e)
{
             
    // Create callback method.
    Graphics.DrawImageAbort imageCallback
        = new Graphics.DrawImageAbort(DrawImageCallback4);
    int imageCallbackData = 1;
             
    // Create image.
    Image newImage = Image.FromFile("SampImag.jpg");
             
    // Create parallelogram for drawing original image.
    PointF ulCorner1 = new PointF(100.0F, 100.0F);
    PointF urCorner1 = new PointF(325.0F, 100.0F);
    PointF llCorner1 = new PointF(150.0F, 250.0F);
    PointF[] destPara1 = {ulCorner1, urCorner1, llCorner1};
             
    // Create rectangle for source image.
    RectangleF srcRect = new RectangleF(50.0F, 50.0F, 150.0F, 150.0F);
    GraphicsUnit units = GraphicsUnit.Pixel;
             
    // Create parallelogram for drawing adjusted image.
    PointF ulCorner2 = new PointF(325.0F, 100.0F);
    PointF urCorner2 = new PointF(550.0F, 100.0F);
    PointF llCorner2 = new PointF(375.0F, 250.0F);
    PointF[] destPara2 = {ulCorner2, urCorner2, llCorner2};
             
    // Draw original image to screen.
    e.Graphics.DrawImage(newImage, destPara1, srcRect, units);
             
    // Create image attributes and set large gamma.
    ImageAttributes imageAttr = new ImageAttributes();
    imageAttr.SetGamma(4.0F);
    try
    {
        checked
        {
             
            // Draw adjusted image to screen.
            e.Graphics.DrawImage(
                newImage,
                destPara2,
                srcRect,
                units,
                imageAttr,
                imageCallback,
                imageCallbackData);
        }
    }
    catch (Exception ex)
    {
        e.Graphics.DrawString(
            ex.ToString(),
            new Font("Arial", 8),
            Brushes.Black,
            new PointF(0, 0));
    }
}

備註

destPoints 參數會指定平行投影的三點。 三個 PointF 結構代表平行投影的左上角、右上角和左下角。 第四個點是從前三個推斷出來的,以形成平行投影。

srcRect 參數會指定要繪製之 image 物件的矩形部分。 此部分會縮放並剪下,以符合 destPoints 參數所指定的平行投影。

具有 callbackcallbackData 參數的這個多載會根據應用程式所決定的準則和數據,提供停止影像繪製的方法。 例如,應用程式可以開始繪製大型影像,而使用者可能會將影像捲動到畫面上,在此情況下,應用程式可能會停止繪圖。

另請參閱

適用於

.NET 9 及其他版本
產品 版本
.NET 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

DrawImage(Image, Rectangle, Int32, Int32, Int32, Int32, GraphicsUnit)

來源:
Graphics.cs
來源:
Graphics.cs
來源:
Graphics.cs
來源:
Graphics.cs
來源:
Graphics.cs

在指定的位置和具有指定大小的指定位置繪製指定 Image 的指定部分。

C#
public void DrawImage (System.Drawing.Image image, System.Drawing.Rectangle destRect, int srcX, int srcY, int srcWidth, int srcHeight, System.Drawing.GraphicsUnit srcUnit);

參數

image
Image

要繪製的 Image

destRect
Rectangle

Rectangle 結構,指定繪製影像的位置和大小。 影像會縮放以符合矩形。

srcX
Int32

要繪製之來源影像部分左上角的 X 座標。

srcY
Int32

要繪製之來源影像部分左上角的 Y 座標。

srcWidth
Int32

要繪製之來源影像部分的寬度。

srcHeight
Int32

要繪製之來源影像部分的高度。

srcUnit
GraphicsUnit

GraphicsUnit 列舉的成員,指定用來判斷來源矩形的量值單位。

例外狀況

image null

範例

下列程式代碼範例是專為搭配 Windows Forms 使用而設計,而且需要 PaintEventArgse,這是 Paint 事件處理程式的參數。 程式代碼會執行下列動作:

  • 從範例資料夾中的 JPEG 檔案 SampImag.jpg 建立映像。

  • 建立要在其中繪製影像的目的地矩形。

  • 建立來源矩形的座標,從中擷取影像的一部分。

  • 將來源矩形的測量單位設定為圖元。

  • 將影像繪製到畫面。

目的矩形的位置會在畫面上找出影像,而來源和目的矩形的大小會決定繪製影像的縮放比例,而來源矩形的大小會決定原始影像繪製到畫面的哪個部分。

C#
private void DrawImageRect4Int(PaintEventArgs e)
{
             
    // Create image.
    Image newImage = Image.FromFile("SampImag.jpg");
             
    // Create rectangle for displaying image.
    Rectangle destRect = new Rectangle(100, 100, 450, 150);
             
    // Create coordinates of rectangle for source image.
    int x = 50;
    int y = 50;
    int width = 150;
    int height = 150;
    GraphicsUnit units = GraphicsUnit.Pixel;
             
    // Draw image to screen.
    e.Graphics.DrawImage(newImage, destRect, x, y, width, height, units);
}

備註

srcXsrcYsrcWidthsrcHeight 參數會指定要繪製之 image 對象的矩形部分。 矩形相對於來源影像的左上角。 此部分會縮放以符合 destRect 參數所指定的矩形。

另請參閱

適用於

.NET 9 及其他版本
產品 版本
.NET 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

DrawImage(Image, Rectangle, Int32, Int32, Int32, Int32, GraphicsUnit, ImageAttributes)

來源:
Graphics.cs
來源:
Graphics.cs
來源:
Graphics.cs
來源:
Graphics.cs
來源:
Graphics.cs

在指定的位置和具有指定大小的指定位置繪製指定 Image 的指定部分。

C#
public void DrawImage (System.Drawing.Image image, System.Drawing.Rectangle destRect, int srcX, int srcY, int srcWidth, int srcHeight, System.Drawing.GraphicsUnit srcUnit, System.Drawing.Imaging.ImageAttributes? imageAttr);
C#
public void DrawImage (System.Drawing.Image image, System.Drawing.Rectangle destRect, int srcX, int srcY, int srcWidth, int srcHeight, System.Drawing.GraphicsUnit srcUnit, System.Drawing.Imaging.ImageAttributes imageAttr);

參數

image
Image

要繪製的 Image

destRect
Rectangle

Rectangle 結構,指定繪製影像的位置和大小。 影像會縮放以符合矩形。

srcX
Int32

要繪製之來源影像部分左上角的 X 座標。

srcY
Int32

要繪製之來源影像部分左上角的 Y 座標。

srcWidth
Int32

要繪製之來源影像部分的寬度。

srcHeight
Int32

要繪製之來源影像部分的高度。

srcUnit
GraphicsUnit

GraphicsUnit 列舉的成員,指定用來判斷來源矩形的量值單位。

imageAttr
ImageAttributes

ImageAttributes,指定 image 物件的重新著色和伽瑪資訊。

例外狀況

image null

範例

下列程式代碼範例是專為搭配 Windows Forms 使用而設計,而且需要 PaintEventArgse,這是 Paint 事件處理程式的參數。 程式代碼會執行下列動作:

  • 從範例資料夾中的 JPEG 檔案 SampImag.jpg 建立映像。

  • 建立要在其中繪製影像的目的地矩形。

  • 建立來源矩形的座標,從中擷取影像的一部分。

  • 將來源矩形的測量單位設定為圖元。

  • 將原始影像繪製到畫面。

  • 建立要在其中繪製調整影像的其他矩形。

  • 建立並設定調整影像的屬性,以具有大於一般伽瑪值。

  • 將調整后的影像繪製到畫面。

針對原始的未調整目的矩形,位置會在畫面上找出影像,而來源和目的矩形的大小會決定繪製影像的縮放比例,而來源矩形的大小會決定原始影像的繪製到畫面的哪個部分。

C#
private void DrawImageRect4IntAtrrib(PaintEventArgs e)
{
             
    // Create image.
    Image newImage = Image.FromFile("SampImag.jpg");
             
    // Create rectangle for displaying original image.
    Rectangle destRect1 = new Rectangle(100, 25, 450, 150);
             
    // Create coordinates of rectangle for source image.
    int x = 50;
    int y = 50;
    int width = 150;
    int height = 150;
    GraphicsUnit units = GraphicsUnit.Pixel;
             
    // Draw original image to screen.
    e.Graphics.DrawImage(newImage, destRect1, x, y, width, height, units);
             
    // Create rectangle for adjusted image.
    Rectangle destRect2 = new Rectangle(100, 175, 450, 150);
             
    // Create image attributes and set large gamma.
    ImageAttributes imageAttr = new ImageAttributes();
    imageAttr.SetGamma(4.0F);
             
    // Draw adjusted image to screen.
    e.Graphics.DrawImage(newImage, destRect2, x, y, width, height, units, imageAttr);
}

備註

srcXsrcYsrcWidthsrcHeight 參數會指定要繪製之 image 對象的矩形部分。 矩形相對於來源影像的左上角。 此部分會縮放以符合 destRect 參數所指定的矩形。

另請參閱

適用於

.NET 9 及其他版本
產品 版本
.NET 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

DrawImage(Image, Rectangle, Single, Single, Single, Single, GraphicsUnit, ImageAttributes)

來源:
Graphics.cs
來源:
Graphics.cs
來源:
Graphics.cs
來源:
Graphics.cs
來源:
Graphics.cs

在指定的位置和具有指定大小的指定位置繪製指定 Image 的指定部分。

C#
public void DrawImage (System.Drawing.Image image, System.Drawing.Rectangle destRect, float srcX, float srcY, float srcWidth, float srcHeight, System.Drawing.GraphicsUnit srcUnit, System.Drawing.Imaging.ImageAttributes? imageAttrs);
C#
public void DrawImage (System.Drawing.Image image, System.Drawing.Rectangle destRect, float srcX, float srcY, float srcWidth, float srcHeight, System.Drawing.GraphicsUnit srcUnit, System.Drawing.Imaging.ImageAttributes imageAttrs);

參數

image
Image

要繪製的 Image

destRect
Rectangle

Rectangle 結構,指定繪製影像的位置和大小。 影像會縮放以符合矩形。

srcX
Single

要繪製之來源影像部分左上角的 X 座標。

srcY
Single

要繪製之來源影像部分左上角的 Y 座標。

srcWidth
Single

要繪製之來源影像部分的寬度。

srcHeight
Single

要繪製之來源影像部分的高度。

srcUnit
GraphicsUnit

GraphicsUnit 列舉的成員,指定用來判斷來源矩形的量值單位。

imageAttrs
ImageAttributes

ImageAttributes,指定 image 物件的重新著色和伽瑪資訊。

例外狀況

image null

範例

下列程式代碼範例是專為搭配 Windows Forms 使用而設計,而且需要 PaintEventArgse,這是 Paint 事件處理程式的參數。 程式代碼會執行下列動作:

  • 從範例資料夾中的 JPEG 檔案 SampImag.jpg 建立映像。

  • 建立要在其中繪製影像的目的地矩形。

  • 建立來源矩形的座標,從中擷取影像的一部分。

  • 將來源矩形的測量單位設定為圖元。

  • 將原始影像繪製到畫面。

  • 建立要在其中繪製調整影像的其他矩形。

  • 建立並設定調整影像的屬性,以具有大於一般伽瑪值。

  • 將調整后的影像繪製到畫面。

針對原始的未調整目的矩形,位置會在畫面上找出影像,而來源和目的矩形的大小會決定繪製影像的縮放比例,而來源矩形的大小會決定原始影像的繪製到畫面的哪個部分。

C#
private void DrawImageRect4FloatAttrib(PaintEventArgs e)
{
             
    // Create image.
    Image newImage = Image.FromFile("SampImag.jpg");
             
    // Create rectangle for displaying original image.
    Rectangle destRect1 = new Rectangle(100, 25, 450, 150);
             
    // Create coordinates of rectangle for source image.
    float x = 50.0F;
    float y = 50.0F;
    float width = 150.0F;
    float height = 150.0F;
    GraphicsUnit units = GraphicsUnit.Pixel;
             
    // Draw original image to screen.
    e.Graphics.DrawImage(newImage, destRect1, x, y, width, height, units);
             
    // Create rectangle for adjusted image.
    Rectangle destRect2 = new Rectangle(100, 175, 450, 150);
             
    // Create image attributes and set large gamma.
    ImageAttributes imageAttr = new ImageAttributes();
    imageAttr.SetGamma(4.0F);
             
    // Draw adjusted image to screen.
    e.Graphics.DrawImage(newImage, destRect2, x, y, width, height, units, imageAttr);
}

備註

srcXsrcYsrcWidthsrcHeight 參數會指定要繪製之 image 對象的矩形部分。 矩形相對於來源影像的左上角。 此部分會縮放以符合 destRect 參數所指定的矩形。

另請參閱

適用於

.NET 9 及其他版本
產品 版本
.NET 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

DrawImage(Image, Rectangle, Int32, Int32, Int32, Int32, GraphicsUnit, ImageAttributes, Graphics+DrawImageAbort)

來源:
Graphics.cs
來源:
Graphics.cs
來源:
Graphics.cs
來源:
Graphics.cs
來源:
Graphics.cs

在指定的位置和具有指定大小的指定位置繪製指定 Image 的指定部分。

C#
public void DrawImage (System.Drawing.Image image, System.Drawing.Rectangle destRect, int srcX, int srcY, int srcWidth, int srcHeight, System.Drawing.GraphicsUnit srcUnit, System.Drawing.Imaging.ImageAttributes? imageAttr, System.Drawing.Graphics.DrawImageAbort? callback);
C#
public void DrawImage (System.Drawing.Image image, System.Drawing.Rectangle destRect, int srcX, int srcY, int srcWidth, int srcHeight, System.Drawing.GraphicsUnit srcUnit, System.Drawing.Imaging.ImageAttributes imageAttr, System.Drawing.Graphics.DrawImageAbort callback);

參數

image
Image

要繪製的 Image

destRect
Rectangle

Rectangle 結構,指定繪製影像的位置和大小。 影像會縮放以符合矩形。

srcX
Int32

要繪製之來源影像部分左上角的 X 座標。

srcY
Int32

要繪製之來源影像部分左上角的 Y 座標。

srcWidth
Int32

要繪製之來源影像部分的寬度。

srcHeight
Int32

要繪製之來源影像部分的高度。

srcUnit
GraphicsUnit

GraphicsUnit 列舉的成員,指定用來判斷來源矩形的量值單位。

imageAttr
ImageAttributes

ImageAttributes,指定 image的重新著色和伽瑪資訊。

callback
Graphics.DrawImageAbort

Graphics.DrawImageAbort 委派,指定要在影像繪製期間呼叫的方法。 這個方法經常呼叫,以檢查是否要根據應用程式判斷的準則停止執行 DrawImage(Image, Rectangle, Int32, Int32, Int32, Int32, GraphicsUnit, ImageAttributes, Graphics+DrawImageAbort) 方法。

例外狀況

image null

範例

下列程式代碼範例是專為搭配 Windows Forms 使用而設計,而且需要 PaintEventArgse,這是 Paint 事件處理程式的參數。 程序代碼會先定義 Graphics.DrawImageAbort 委派的回呼方法;定義簡單明瞭,只是測試 DrawImage 方法是否以 null callBackData 參數呼叫它。 此範例的主體會執行下列動作:

  • 建立 Graphics.DrawImageAbort 回呼方法的實例。

  • 從範例資料夾中的 JPEG 檔案 SampImag.jpg 建立映像。

  • 建立點,以定義用來繪製影像的目的矩形。

  • 建立來源矩形,以選取要繪製的影像部分。

  • 將圖形繪圖單位設定為圖元。

  • 將原始影像繪製到畫面。

  • 建立要在其中繪製調整影像的其他目的地矩形。

  • 建立並設定調整影像的屬性,以具有大於一般伽瑪值。

  • 將調整后的影像繪製到畫面。

針對原始的未調整目的矩形,位置會在畫面上找出影像,而來源矩形的大小和目的矩形的大小和形狀會決定繪製影像的縮放比例。

由於此範例使用多載未傳遞 callBackData 參數,因此 Graphics.DrawImageAbort 回呼會傳回 true,這會導致 DrawImage 方法結束,而範例中包含的例外狀況處理程式代碼會列印出例外狀況文字,而不是繪製影像。

C#

// Define DrawImageAbort callback method.
private bool DrawImageCallback5(IntPtr callBackData)
{
             
    // Test for call that passes callBackData parameter.
    if(callBackData==IntPtr.Zero)
    {
             
        // If no callBackData passed, abort DrawImage method.
        return true;
    }
    else
    {
             
        // If callBackData passed, continue DrawImage method.
        return false;
    }
}
private void DrawImageRect4IntAtrribAbort(PaintEventArgs e)
{
             
    // Create callback method.
    Graphics.DrawImageAbort imageCallback
        = new Graphics.DrawImageAbort(DrawImageCallback5);
             
    // Create image.
    Image newImage = Image.FromFile("SampImag.jpg");
             
    // Create rectangle for displaying original image.
    Rectangle destRect1 = new Rectangle(100, 25, 450, 150);
             
    // Create coordinates of rectangle for source image.
    int x = 50;
    int y = 50;
    int width = 150;
    int height = 150;
    GraphicsUnit units = GraphicsUnit.Pixel;
             
    // Draw original image to screen.
    e.Graphics.DrawImage(newImage, destRect1, x, y, width, height, units);
             
    // Create rectangle for adjusted image.
    Rectangle destRect2 = new Rectangle(100, 175, 450, 150);
             
    // Create image attributes and set large gamma.
    ImageAttributes imageAttr = new ImageAttributes();
    imageAttr.SetGamma(4.0F);
    try
    {
        checked
        {
             
            // Draw adjusted image to screen.
            e.Graphics.DrawImage(
                newImage,
                destRect2,
                x, y,
                width, height,
                units,
                imageAttr,
                imageCallback);
        }
    }
    catch (Exception ex)
    {
        e.Graphics.DrawString(
            ex.ToString(),
            new Font("Arial", 8),
            Brushes.Black,
            new PointF(0, 0));
    }
}

備註

srcXsrcYsrcWidthsrcHeight 參數會指定要繪製之 image 對象的矩形部分。 矩形相對於來源影像的左上角。 此部分會縮放成符合 destRect 物件所指定的矩形。

具有 callback 參數的這個多載提供一旦根據應用程式所決定準則啟動影像時停止繪製的方法。 例如,應用程式可以開始繪製大型影像,而使用者可能會將影像捲動到畫面上,在此情況下,應用程式可能會停止繪圖。

另請參閱

適用於

.NET 9 及其他版本
產品 版本
.NET 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

DrawImage(Image, Rectangle, Single, Single, Single, Single, GraphicsUnit, ImageAttributes, Graphics+DrawImageAbort)

來源:
Graphics.cs
來源:
Graphics.cs
來源:
Graphics.cs
來源:
Graphics.cs
來源:
Graphics.cs

在指定的位置和具有指定大小的指定位置繪製指定 Image 的指定部分。

C#
public void DrawImage (System.Drawing.Image image, System.Drawing.Rectangle destRect, float srcX, float srcY, float srcWidth, float srcHeight, System.Drawing.GraphicsUnit srcUnit, System.Drawing.Imaging.ImageAttributes? imageAttrs, System.Drawing.Graphics.DrawImageAbort? callback);
C#
public void DrawImage (System.Drawing.Image image, System.Drawing.Rectangle destRect, float srcX, float srcY, float srcWidth, float srcHeight, System.Drawing.GraphicsUnit srcUnit, System.Drawing.Imaging.ImageAttributes imageAttrs, System.Drawing.Graphics.DrawImageAbort callback);

參數

image
Image

要繪製的 Image

destRect
Rectangle

Rectangle 結構,指定繪製影像的位置和大小。 影像會縮放以符合矩形。

srcX
Single

要繪製之來源影像部分左上角的 X 座標。

srcY
Single

要繪製之來源影像部分左上角的 Y 座標。

srcWidth
Single

要繪製之來源影像部分的寬度。

srcHeight
Single

要繪製之來源影像部分的高度。

srcUnit
GraphicsUnit

GraphicsUnit 列舉的成員,指定用來判斷來源矩形的量值單位。

imageAttrs
ImageAttributes

ImageAttributes,指定 image 物件的重新著色和伽瑪資訊。

callback
Graphics.DrawImageAbort

Graphics.DrawImageAbort 委派,指定要在影像繪製期間呼叫的方法。 這個方法經常呼叫,以檢查是否要根據應用程式判斷的準則停止執行 DrawImage(Image, Rectangle, Single, Single, Single, Single, GraphicsUnit, ImageAttributes, Graphics+DrawImageAbort) 方法。

例外狀況

image null

範例

下列程式代碼範例是專為搭配 Windows Forms 使用而設計,而且需要 PaintEventArgse,這是 Paint 事件處理程式的參數。 程序代碼會先定義 Graphics.DrawImageAbort 委派的回呼方法;定義簡單明瞭,只是測試 DrawImage 方法是否以 null callBackData 參數呼叫它。 此範例的主體會執行下列動作:

  • 建立 Graphics.DrawImageAbort 回呼方法的實例。

  • 從範例資料夾中的 JPEG 檔案 SampImag.jpg 建立映像。

  • 建立點,以定義用來繪製影像的目的矩形。

  • 建立來源矩形,以選取要繪製的影像部分。

  • 將圖形繪圖單位設定為圖元。

  • 將原始影像繪製到畫面。

  • 建立要在其中繪製調整影像的其他目的地矩形。

  • 建立並設定調整影像的屬性,以具有大於一般伽瑪值。

  • 將調整后的影像繪製到畫面。

針對原始的未調整目的矩形,位置會在畫面上找出影像,而來源矩形的大小和目的矩形的大小和形狀會決定繪製影像的縮放比例。

由於此範例使用多載未傳遞 callBackData 參數,因此 Graphics.DrawImageAbort 回呼會傳回 true,這會導致 DrawImage 方法結束,而範例中包含的例外狀況處理程式代碼會列印出例外狀況文字,而不是繪製影像。

C#

// Define DrawImageAbort callback method.
private bool DrawImageCallback7(IntPtr callBackData)
{
             
    // Test for call that passes callBackData parameter.
    if(callBackData==IntPtr.Zero)
    {
             
        // If no callBackData passed, abort DrawImage method.
        return true;
    }
    else
    {
             
        // If callBackData passed, continue DrawImage method.
        return false;
    }
}
private void DrawImageRect4FloatAttribAbort(PaintEventArgs e)
{
             
    // Create callback method.
    Graphics.DrawImageAbort imageCallback
        = new Graphics.DrawImageAbort(DrawImageCallback7);
             
    // Create image.
    Image newImage = Image.FromFile("SampImag.jpg");
             
    // Create rectangle for displaying original image.
    Rectangle destRect1 = new Rectangle(100, 25, 450, 150);
             
    // Create coordinates of rectangle for source image.
    float x = 50.0F;
    float y = 50.0F;
    float width = 150.0F;
    float height = 150.0F;
    GraphicsUnit units = GraphicsUnit.Pixel;
             
    // Draw original image to screen.
    e.Graphics.DrawImage(newImage, destRect1, x, y, width, height, units);
             
    // Create rectangle for adjusted image.
    Rectangle destRect2 = new Rectangle(100, 175, 450, 150);
             
    // Create image attributes and set large gamma.
    ImageAttributes imageAttr = new ImageAttributes();
    imageAttr.SetGamma(4.0F);
    try
    {
        checked
        {
             
            // Draw adjusted image to screen.
            e.Graphics.DrawImage(
                newImage,
                destRect2,
                x, y,
                width, height,
                units,
                imageAttr,
                imageCallback);
        }
    }
    catch (Exception ex)
    {
        e.Graphics.DrawString(
            ex.ToString(),
            new Font("Arial", 8),
            Brushes.Black,
            new PointF(0, 0));
    }
}

備註

srcXsrcYsrcWidthsrcHeight 參數會指定要繪製之 image 對象的矩形部分。 矩形相對於來源影像的左上角。 此部分會縮放以符合 destRect 參數所指定的矩形。

具有 callback 參數的這個多載提供一旦根據應用程式所決定準則啟動影像時停止繪製的方法。 例如,應用程式可以開始繪製大型影像,而使用者可能會將影像捲動到畫面上,在此情況下,應用程式可能會停止繪圖。

另請參閱

適用於

.NET 9 及其他版本
產品 版本
.NET 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

DrawImage(Image, Rectangle, Int32, Int32, Int32, Int32, GraphicsUnit, ImageAttributes, Graphics+DrawImageAbort, IntPtr)

來源:
Graphics.cs
來源:
Graphics.cs
來源:
Graphics.cs
來源:
Graphics.cs
來源:
Graphics.cs

在指定的位置和具有指定大小的指定位置繪製指定 Image 的指定部分。

C#
public void DrawImage (System.Drawing.Image image, System.Drawing.Rectangle destRect, int srcX, int srcY, int srcWidth, int srcHeight, System.Drawing.GraphicsUnit srcUnit, System.Drawing.Imaging.ImageAttributes? imageAttrs, System.Drawing.Graphics.DrawImageAbort? callback, IntPtr callbackData);
C#
public void DrawImage (System.Drawing.Image image, System.Drawing.Rectangle destRect, int srcX, int srcY, int srcWidth, int srcHeight, System.Drawing.GraphicsUnit srcUnit, System.Drawing.Imaging.ImageAttributes imageAttrs, System.Drawing.Graphics.DrawImageAbort callback, IntPtr callbackData);

參數

image
Image

要繪製的 Image

destRect
Rectangle

Rectangle 結構,指定繪製影像的位置和大小。 影像會縮放以符合矩形。

srcX
Int32

要繪製之來源影像部分左上角的 X 座標。

srcY
Int32

要繪製之來源影像部分左上角的 Y 座標。

srcWidth
Int32

要繪製之來源影像部分的寬度。

srcHeight
Int32

要繪製之來源影像部分的高度。

srcUnit
GraphicsUnit

GraphicsUnit 列舉的成員,指定用來判斷來源矩形的量值單位。

imageAttrs
ImageAttributes

ImageAttributes,指定 image 物件的重新著色和伽瑪資訊。

callback
Graphics.DrawImageAbort

Graphics.DrawImageAbort 委派,指定要在影像繪製期間呼叫的方法。 這個方法經常呼叫,以檢查是否要根據應用程式判斷的準則停止執行 DrawImage(Image, Rectangle, Int32, Int32, Int32, Int32, GraphicsUnit, ImageAttributes, Graphics+DrawImageAbort, IntPtr) 方法。

callbackData
IntPtr

值,指定檢查是否停止執行 DrawImage 方法時要使用的 Graphics.DrawImageAbort 委派的其他數據。

例外狀況

image null

範例

下列程式代碼範例是專為搭配 Windows Forms 使用而設計,而且需要 PaintEventArgse,這是 Paint 事件處理程式的參數。 程序代碼會先定義 Graphics.DrawImageAbort 委派的回呼方法;定義簡單明瞭,只是測試 DrawImage 方法是否以 null callBackData 參數呼叫它。 此範例的主體會執行下列動作:

  • 建立 Graphics.DrawImageAbort 回呼方法的實例。

  • 從範例資料夾中的 JPEG 檔案 SampImag.jpg 建立映像。

  • 建立點,以定義用來繪製影像的目的矩形。

  • 建立來源矩形,以選取要繪製的影像部分。

  • 將圖形繪圖單位設定為圖元。

  • 將原始影像繪製到畫面。

  • 建立要在其中繪製調整影像的其他目的地矩形。

  • 建立並設定調整影像的屬性,以具有大於一般伽瑪值。

  • 將調整后的影像繪製到畫面。

針對原始的未調整目的矩形,位置會在畫面上找出影像,而來源矩形的大小和目的矩形的大小和形狀會決定繪製影像的縮放比例。

因為這個範例會使用傳遞 callBackData 參數的多載,Graphics.DrawImageAbort 回呼會傳回 false,這會導致 DrawImage 方法繼續,而此範例會將調整后的影像繪製到畫面。

C#

// Define DrawImageAbort callback method.
private bool DrawImageCallback6(IntPtr callBackData)
{
             
    // Test for call that passes callBackData parameter.
    if(callBackData==IntPtr.Zero)
    {
             
        // If no callBackData passed, abort DrawImage method.
        return true;
    }
    else
    {
             
        // If callBackData passed, continue DrawImage method.
        return false;
    }
}
private void DrawImageRect4IntAtrribAbortData(PaintEventArgs e)
{
             
    // Create callback method.
    Graphics.DrawImageAbort imageCallback
        = new Graphics.DrawImageAbort(DrawImageCallback6);
    IntPtr imageCallbackData = new IntPtr(1);
             
    // Create image.
    Image newImage = Image.FromFile("SampImag.jpg");
             
    // Create rectangle for displaying original image.
    Rectangle destRect1 = new Rectangle(100, 25, 450, 150);
             
    // Create coordinates of rectangle for source image.
    int x = 50;
    int y = 50;
    int width = 150;
    int height = 150;
    GraphicsUnit units = GraphicsUnit.Pixel;
             
    // Draw original image to screen.
    e.Graphics.DrawImage(newImage, destRect1, x, y, width, height, units);
             
    // Create rectangle for adjusted image.
    Rectangle destRect2 = new Rectangle(100, 175, 450, 150);
             
    // Create image attributes and set large gamma.
    ImageAttributes imageAttr = new ImageAttributes();
    imageAttr.SetGamma(4.0F);
    try
    {
        checked
        {
             
            // Draw adjusted image to screen.
            e.Graphics.DrawImage(
                newImage,
                destRect2,
                x, y,
                width, height,
                units,
                imageAttr,
                imageCallback,
                imageCallbackData);
        }
    }
    catch (Exception ex)
    {
        e.Graphics.DrawString(
            ex.ToString(),
            new Font("Arial", 8),
            Brushes.Black,
            new PointF(0, 0));
    }
}

備註

srcXsrcYsrcWidthsrcHeight 參數會指定要繪製之 image 對象的矩形部分。 矩形相對於來源影像的左上角。 此部分會縮放以符合 destRect 參數所指定的矩形。

具有 callbackcallbackData 參數的這個多載會根據應用程式所決定的準則和數據,提供停止影像繪製的方法。 例如,應用程式可以開始繪製大型影像,而使用者可能會將影像捲動到畫面上,在此情況下,應用程式可能會停止繪圖。

另請參閱

適用於

.NET 9 及其他版本
產品 版本
.NET 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

DrawImage(Image, Rectangle, Single, Single, Single, Single, GraphicsUnit, ImageAttributes, Graphics+DrawImageAbort, IntPtr)

來源:
Graphics.cs
來源:
Graphics.cs
來源:
Graphics.cs
來源:
Graphics.cs
來源:
Graphics.cs

在指定的位置和具有指定大小的指定位置繪製指定 Image 的指定部分。

C#
public void DrawImage (System.Drawing.Image image, System.Drawing.Rectangle destRect, float srcX, float srcY, float srcWidth, float srcHeight, System.Drawing.GraphicsUnit srcUnit, System.Drawing.Imaging.ImageAttributes? imageAttrs, System.Drawing.Graphics.DrawImageAbort? callback, IntPtr callbackData);
C#
public void DrawImage (System.Drawing.Image image, System.Drawing.Rectangle destRect, float srcX, float srcY, float srcWidth, float srcHeight, System.Drawing.GraphicsUnit srcUnit, System.Drawing.Imaging.ImageAttributes imageAttrs, System.Drawing.Graphics.DrawImageAbort callback, IntPtr callbackData);

參數

image
Image

要繪製的 Image

destRect
Rectangle

Rectangle 結構,指定繪製影像的位置和大小。 影像會縮放以符合矩形。

srcX
Single

要繪製之來源影像部分左上角的 X 座標。

srcY
Single

要繪製之來源影像部分左上角的 Y 座標。

srcWidth
Single

要繪製之來源影像部分的寬度。

srcHeight
Single

要繪製之來源影像部分的高度。

srcUnit
GraphicsUnit

GraphicsUnit 列舉的成員,指定用來判斷來源矩形的量值單位。

imageAttrs
ImageAttributes

ImageAttributes,指定 image 物件的重新著色和伽瑪資訊。

callback
Graphics.DrawImageAbort

Graphics.DrawImageAbort 委派,指定要在影像繪製期間呼叫的方法。 這個方法經常呼叫,以檢查是否要根據應用程式判斷的準則停止執行 DrawImage(Image, Rectangle, Single, Single, Single, Single, GraphicsUnit, ImageAttributes, Graphics+DrawImageAbort, IntPtr) 方法。

callbackData
IntPtr

值,指定檢查是否停止執行 DrawImage 方法時要使用的 Graphics.DrawImageAbort 委派的其他數據。

例外狀況

image null

範例

下列程式代碼範例是專為搭配 Windows Forms 使用而設計,而且需要 PaintEventArgse,這是 Paint 事件處理程式的參數。 程序代碼會先定義 Graphics.DrawImageAbort 委派的回呼方法;定義簡單明瞭,只是測試 DrawImage 方法是否以 null callBackData 參數呼叫它。 此範例的主體會執行下列動作:

  • 建立 Graphics.DrawImageAbort 回呼方法的實例。

  • 從範例資料夾中的 JPEG 檔案 SampImag.jpg 建立映像。

  • 建立點,以定義用來繪製影像的目的矩形。

  • 建立來源矩形,以選取要繪製的影像部分。

  • 將圖形繪圖單位設定為圖元。

  • 將原始影像繪製到畫面。

  • 建立要在其中繪製調整影像的其他目的地矩形。

  • 建立並設定調整影像的屬性,以具有大於一般伽瑪值。

  • 將調整后的影像繪製到畫面。

針對原始的未調整目的矩形,位置會在畫面上找出影像,而來源矩形的大小和目的矩形的大小和形狀會決定繪製影像的縮放比例。

因為這個範例會使用傳遞 callBackData 參數的多載,Graphics.DrawImageAbort 回呼會傳回 false,這會導致 DrawImage 方法繼續,而此範例會將調整后的影像繪製到畫面。

C#

// Define DrawImageAbort callback method.
private bool DrawImageCallback8(IntPtr callBackData)
{
             
    // Test for call that passes callBackData parameter.
    if(callBackData==IntPtr.Zero)
    {
             
        // If no callBackData passed, abort DrawImage method.
        return true;
    }
    else
    {
             
        // If callBackData passed, continue DrawImage method.
        return false;
    }
}
public void DrawImageRect4FloatAttribAbortData(PaintEventArgs e)
{
             
    // Create callback method.
    Graphics.DrawImageAbort imageCallback
        = new Graphics.DrawImageAbort(DrawImageCallback8);
    IntPtr imageCallbackData = new IntPtr(1);
             
    // Create image.
    Image newImage = Image.FromFile("SampImag.jpg");
             
    // Create rectangle for displaying original image.
    Rectangle destRect1 = new Rectangle(100, 25, 450, 150);
             
    // Create coordinates of rectangle for source image.
    float x = 50.0F;
    float y = 50.0F;
    float width = 150.0F;
    float height = 150.0F;
    GraphicsUnit units = GraphicsUnit.Pixel;
             
    // Draw original image to screen.
    e.Graphics.DrawImage(newImage, destRect1, x, y, width, height, units);
             
    // Create rectangle for adjusted image.
    Rectangle destRect2 = new Rectangle(100, 175, 450, 150);
             
    // Create image attributes and set large gamma.
    ImageAttributes imageAttr = new ImageAttributes();
    imageAttr.SetGamma(4.0F);
             
    // Draw adjusted image to screen.
    try
    {
        checked
        {
             
            // Draw adjusted image to screen.
            e.Graphics.DrawImage(
                newImage,
                destRect2,
                x, y,
                width, height,
                units,
                imageAttr,
                imageCallback,
                imageCallbackData);
        }
    }
    catch (Exception ex)
    {
        e.Graphics.DrawString(
            ex.ToString(),
            new Font("Arial", 8),
            Brushes.Black,
            new PointF(0, 0));
    }
}

備註

srcXsrcYsrcWidthsrcHeight 參數會指定要繪製之 image 對象的矩形部分。 矩形相對於來源影像的左上角。 此部分會縮放以符合 destRect 參數所指定的矩形。

具有 callbackcallbackData 參數的這個多載會根據應用程式所決定的準則和數據,提供停止影像繪製的方法。 例如,應用程式可以開始繪製大型影像,而使用者可能會將影像捲動到畫面上,在此情況下,應用程式可能會停止繪圖。

另請參閱

適用於

.NET 9 及其他版本
產品 版本
.NET 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

DrawImage(Image, Rectangle, Single, Single, Single, Single, GraphicsUnit)

來源:
Graphics.cs
來源:
Graphics.cs
來源:
Graphics.cs
來源:
Graphics.cs
來源:
Graphics.cs

在指定的位置和具有指定大小的指定位置繪製指定 Image 的指定部分。

C#
public void DrawImage (System.Drawing.Image image, System.Drawing.Rectangle destRect, float srcX, float srcY, float srcWidth, float srcHeight, System.Drawing.GraphicsUnit srcUnit);

參數

image
Image

要繪製的 Image

destRect
Rectangle

Rectangle 結構,指定繪製影像的位置和大小。 影像會縮放以符合矩形。

srcX
Single

要繪製之來源影像部分左上角的 X 座標。

srcY
Single

要繪製之來源影像部分左上角的 Y 座標。

srcWidth
Single

要繪製之來源影像部分的寬度。

srcHeight
Single

要繪製之來源影像部分的高度。

srcUnit
GraphicsUnit

GraphicsUnit 列舉的成員,指定用來判斷來源矩形的量值單位。

例外狀況

image null

範例

下列程式代碼範例是專為搭配 Windows Forms 使用而設計,而且需要 PaintEventArgse,這是 Paint 事件處理程式的參數。 程式代碼會執行下列動作:

  • 從範例資料夾中的 JPEG 檔案 SampImag.jpg 建立映像。

  • 建立要在其中繪製影像之目的矩形的座標。

  • 建立來源矩形,從中擷取影像的一部分。

  • 將來源矩形的測量單位設定為圖元。

  • 將影像繪製到畫面。

目的矩形的位置會在畫面上找出影像、來源和目的矩形的大小決定繪製影像的縮放比例,而來源矩形的大小會決定原始影像繪製到畫面的哪個部分。

C#
private void DrawImageRect4Float(PaintEventArgs e)
{
             
    // Create image.
    Image newImage = Image.FromFile("SampImag.jpg");
             
    // Create rectangle for displaying image.
    Rectangle destRect = new Rectangle(100, 100, 450, 150);
             
    // Create coordinates of rectangle for source image.
    float x = 50.0F;
    float y = 50.0F;
    float width = 150.0F;
    float height = 150.0F;
    GraphicsUnit units = GraphicsUnit.Pixel;
             
    // Draw image to screen.
    e.Graphics.DrawImage(newImage, destRect, x, y, width, height, units);
}

備註

srcXsrcYsrcWidthsrcHeight 參數會指定要繪製之 image 對象的矩形部分。 矩形相對於來源影像的左上角。 此部分會縮放以符合 destRect 參數所指定的矩形。

另請參閱

適用於

.NET 9 及其他版本
產品 版本
.NET 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

DrawImage(Image, Int32, Int32, Int32, Int32)

來源:
Graphics.cs
來源:
Graphics.cs
來源:
Graphics.cs
來源:
Graphics.cs
來源:
Graphics.cs

在指定的位置繪製指定的 Image,並具有指定的大小。

C#
public void DrawImage (System.Drawing.Image image, int x, int y, int width, int height);

參數

image
Image

要繪製的 Image

x
Int32

繪製影像左上角的 X 座標。

y
Int32

繪製影像左上角的 Y 座標。

width
Int32

繪製影像的寬度。

height
Int32

繪製影像的高度。

例外狀況

image null

範例

下列程式代碼範例是專為搭配 Windows Forms 使用而設計,而且需要 PaintEventArgse,這是 Paint 事件處理程式的參數。 程式代碼會執行下列動作:

  • 從範例資料夾中的 JPEG 檔案 SampImag.jpg 建立映像。

  • 建立要在其中繪製影像之矩形的位置和大小。

  • 將影像繪製到畫面。

矩形的位置會在畫面上找出影像,而原始影像的大小和矩形的大小會決定繪製影像的縮放比例。

C#
public void DrawImage4Int(PaintEventArgs e)
{
             
    // Create image.
    Image newImage = Image.FromFile("SampImag.jpg");
             
    // Create coordinates for upper-left corner.
             
    // of image and for size of image.
    int x = 100;
    int y = 100;
    int width = 450;
    int height = 150;
             
    // Draw image to screen.
    e.Graphics.DrawImage(newImage, x, y, width, height);
}

備註

xywidthheight 參數所定義的矩形會決定繪製影像的位置和大小。

另請參閱

適用於

.NET 9 及其他版本
產品 版本
.NET 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

DrawImage(Image, Single, Single, Single, Single)

來源:
Graphics.cs
來源:
Graphics.cs
來源:
Graphics.cs
來源:
Graphics.cs
來源:
Graphics.cs

在指定的位置繪製指定的 Image,並具有指定的大小。

C#
public void DrawImage (System.Drawing.Image image, float x, float y, float width, float height);

參數

image
Image

要繪製的 Image

x
Single

繪製影像左上角的 X 座標。

y
Single

繪製影像左上角的 Y 座標。

width
Single

繪製影像的寬度。

height
Single

繪製影像的高度。

例外狀況

image null

範例

下列程式代碼範例是專為搭配 Windows Forms 使用而設計,而且需要 PaintEventArgse,這是 Paint 事件處理程式的參數。 程式代碼會執行下列動作:

  • 從範例資料夾中的 JPEG 檔案 SampImag.jpg 建立映像。

  • 建立要在其中繪製影像之矩形的位置和大小。

  • 將影像繪製到畫面。

矩形的位置會在畫面上找出影像,而原始影像的大小和矩形的大小會決定繪製影像的縮放比例。

C#
public void DrawImage4Float(PaintEventArgs e)
{
             
    // Create image.
    Image newImage = Image.FromFile("SampImag.jpg");
             
    // Create coordinates for upper-left corner.
             
    // of image and for size of image.
    float x = 100.0F;
    float y = 100.0F;
    float width = 450.0F;
    float height = 150.0F;
             
    // Draw image to screen.
    e.Graphics.DrawImage(newImage, x, y, width, height);
}

備註

xywidthheight 參數所定義的矩形會決定繪製影像的位置和大小。

另請參閱

適用於

.NET 9 及其他版本
產品 版本
.NET 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

DrawImage(Image, PointF[], RectangleF, GraphicsUnit, ImageAttributes)

來源:
Graphics.cs
來源:
Graphics.cs
來源:
Graphics.cs
來源:
Graphics.cs
來源:
Graphics.cs

在指定的位置和具有指定大小的指定位置繪製指定 Image 的指定部分。

C#
public void DrawImage (System.Drawing.Image image, System.Drawing.PointF[] destPoints, System.Drawing.RectangleF srcRect, System.Drawing.GraphicsUnit srcUnit, System.Drawing.Imaging.ImageAttributes? imageAttr);
C#
public void DrawImage (System.Drawing.Image image, System.Drawing.PointF[] destPoints, System.Drawing.RectangleF srcRect, System.Drawing.GraphicsUnit srcUnit, System.Drawing.Imaging.ImageAttributes imageAttr);

參數

image
Image

要繪製的 Image

destPoints
PointF[]

定義平行投影之三個 PointF 結構的陣列。

srcRect
RectangleF

RectangleF 結構,指定要繪製之 image 物件的部分。

srcUnit
GraphicsUnit

GraphicsUnit 列舉的成員,指定 srcRect 參數所使用的量值單位。

imageAttr
ImageAttributes

ImageAttributes,指定 image 物件的重新著色和伽瑪資訊。

例外狀況

image null

範例

下列程式代碼範例是專為搭配 Windows Forms 使用而設計,而且需要 PaintEventArgse,這是 Paint 事件處理程式的參數。 程式代碼會執行下列動作:

  • 從範例資料夾中的 JPEG 檔案 SampImag.jpg 建立映像。

  • 建立點,以定義要在其中繪製影像的目的地平行投影。

  • 建立來源矩形,從中擷取影像的一部分。

  • 將來源矩形的測量單位設定為圖元。

  • 將原始影像繪製到畫面。

  • 建立額外的平行投影,在其中繪製調整后的影像。

  • 建立並設定調整影像的屬性,以具有大於一般伽瑪值。

  • 將調整后的影像繪製到畫面。

針對原始的未調整目的地平行投影,位置會在畫面上找出影像、來源矩形的大小和目的平行投影的大小和形狀,決定繪製影像的縮放和剪下,而矩形的大小會決定原始影像的繪製到螢幕的哪個部分。

C#
private void DrawImageParaFRectFAttrib(PaintEventArgs e)
{
             
    // Create image.
    Image newImage = Image.FromFile("SampImag.jpg");
             
    // Create parallelogram for drawing original image.
    PointF ulCorner1 = new PointF(100.0F, 100.0F);
    PointF urCorner1 = new PointF(325.0F, 100.0F);
    PointF llCorner1 = new PointF(150.0F, 250.0F);
    PointF[] destPara1 = {ulCorner1, urCorner1, llCorner1};
             
    // Create rectangle for source image.
    RectangleF srcRect = new RectangleF(50.0F, 50.0F, 150.0F, 150.0F);
    GraphicsUnit units = GraphicsUnit.Pixel;
             
    // Create parallelogram for drawing adjusted image.
    PointF ulCorner2 = new PointF(325.0F, 100.0F);
    PointF urCorner2 = new PointF(550.0F, 100.0F);
    PointF llCorner2 = new PointF(375.0F, 250.0F);
    PointF[] destPara2 = {ulCorner2, urCorner2, llCorner2};
             
    // Draw original image to screen.
    e.Graphics.DrawImage(newImage, destPara1, srcRect, units);
             
    // Create image attributes and set large gamma.
    ImageAttributes imageAttr = new ImageAttributes();
    imageAttr.SetGamma(4.0F);
             
    // Draw adjusted image to screen.
    e.Graphics.DrawImage(newImage, destPara2, srcRect, units, imageAttr);
}

備註

destPoints 參數會指定平行投影的三點。 三個 PointF 結構代表平行投影的左上角、右上角和左下角。 第四個點是從前三個推斷出來的,以形成平行投影。

srcRect 參數會指定要繪製之 image 物件的矩形部分。 此部分會縮放並剪下,以符合 destPoints 參數所指定的平行投影。

另請參閱

適用於

.NET 9 及其他版本
產品 版本
.NET 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

DrawImage(Image, Effect)

來源:
Graphics.cs
來源:
Graphics.cs
C#
public void DrawImage (System.Drawing.Image image, System.Drawing.Imaging.Effects.Effect effect);

參數

image
Image
effect
Effect

適用於

.NET 9 和 Windows Desktop 9
產品 版本
.NET 9
Windows Desktop 9

DrawImage(Image, Point)

來源:
Graphics.cs
來源:
Graphics.cs
來源:
Graphics.cs
來源:
Graphics.cs
來源:
Graphics.cs

使用指定位置的原始實體大小,繪製指定的 Image

C#
public void DrawImage (System.Drawing.Image image, System.Drawing.Point point);

參數

image
Image

要繪製的 Image

point
Point

Point 結構,表示繪製影像左上角的位置。

例外狀況

image null

範例

下列程式代碼範例是專為搭配 Windows Forms 使用而設計,而且需要 PaintEventArgse,這是 Paint 事件處理程式的參數。 程式代碼會執行下列動作:

  • 從範例資料夾中的 JPEG 檔案 SampImag.jpg 建立映像。

  • 建立要繪製影像左上角的點。

  • 將未調整的影像繪製到畫面。

C#
private void DrawImagePoint(PaintEventArgs e)
{         
    // Create image.
    Image newImage = Image.FromFile("SampImag.jpg");
             
    // Create Point for upper-left corner of image.
    Point ulCorner = new Point(100, 100);
             
    // Draw image to screen.
    e.Graphics.DrawImage(newImage, ulCorner);
}

備註

Image 會儲存圖元寬度的值,以及水平解析度的值(每英吋點)。 以英吋為單位的影像實體寬度是像素寬度除以水平解析度。 例如,圖元寬度為 216 的影像,而水平解析度為每英吋 72 個點,其實體寬度為 3 英吋。 類似的備註適用於圖元高度和實體高度。

這個方法會使用其實體大小繪製影像,因此不論顯示裝置的解析度(每英吋點)為何,影像的大小都會以英吋為單位正確。 例如,假設影像的圖元寬度為 216,而水平解析度為每英吋 72 點。 如果您呼叫此方法,在解析度為每英吋 96 點的裝置上繪製該影像,轉譯影像的圖元寬度會是 (216/72)*96 = 288。

另請參閱

適用於

.NET 9 及其他版本
產品 版本
.NET 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

DrawImage(Image, Point[])

來源:
Graphics.cs
來源:
Graphics.cs
來源:
Graphics.cs
來源:
Graphics.cs
來源:
Graphics.cs

在指定的位置繪製指定的 Image,並具有指定的圖案和大小。

C#
public void DrawImage (System.Drawing.Image image, System.Drawing.Point[] destPoints);

參數

image
Image

要繪製的 Image

destPoints
Point[]

定義平行投影之三個 Point 結構的陣列。

例外狀況

image null

範例

下列程式代碼範例是專為搭配 Windows Forms 使用而設計,而且需要 PaintEventArgse,這是 Paint 事件處理程式的參數。 程式代碼會執行下列動作:

  • 從範例資料夾中的 JPEG 檔案 SampImag.jpg 建立映像。

  • 建立點,以定義用來繪製影像的平行投影。

  • 將影像繪製到畫面。

平行投影的位置會在畫面上找出影像,而原始影像的大小和平行投影的大小和形狀會決定繪製影像的縮放和剪下。

C#
private void DrawImagePara(PaintEventArgs e)
{
    // Create image.
    Image newImage = Image.FromFile("SampImag.jpg");
             
    // Create parallelogram for drawing image.
    Point ulCorner = new Point(100, 100);
    Point urCorner = new Point(550, 100);
    Point llCorner = new Point(150, 250);
    Point[] destPara = {ulCorner, urCorner, llCorner};
             
    // Draw image to screen.
    e.Graphics.DrawImage(newImage, destPara);
}

備註

destPoints 參數會指定平行投影的三點。 三個 Point 結構代表平行投影的左上角、右上角和左下角。 第四個點是從前三個推斷出來的,以形成平行投影。

image 參數所代表的影像會縮放和剪下,以符合 destPoints 參數所指定的平行投影圖形。

另請參閱

適用於

.NET 9 及其他版本
產品 版本
.NET 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

DrawImage(Image, PointF)

來源:
Graphics.cs
來源:
Graphics.cs
來源:
Graphics.cs
來源:
Graphics.cs
來源:
Graphics.cs

使用指定位置的原始實體大小,繪製指定的 Image

C#
public void DrawImage (System.Drawing.Image image, System.Drawing.PointF point);

參數

image
Image

要繪製的 Image

point
PointF

PointF 結構,表示繪製影像的左上角。

例外狀況

image null

範例

下列程式代碼範例是專為搭配 Windows Forms 使用而設計,而且需要 PaintEventArgse,這是 Paint 事件處理程式的參數。 程式代碼會執行下列動作:

  • 從範例資料夾中的 JPEG 檔案 SampImag.jpg 建立映像。

  • 建立要繪製影像左上角的點。

  • 將未調整的影像繪製到畫面。

C#
private void DrawImagePointF(PaintEventArgs e)
{
             
    // Create image.
    Image newImage = Image.FromFile("SampImag.jpg");
             
    // Create point for upper-left corner of image.
    PointF ulCorner = new PointF(100.0F, 100.0F);
             
    // Draw image to screen.
    e.Graphics.DrawImage(newImage, ulCorner);
}

備註

Image 會儲存圖元寬度的值,以及水平解析度的值(每英吋點)。 以英吋為單位的影像實體寬度是像素寬度除以水平解析度。 例如,圖元寬度為 216 的影像,而水平解析度為每英吋 72 個點,其實體寬度為 3 英吋。 類似的備註適用於圖元高度和實體高度。

這個方法會使用其實體大小繪製影像,因此不論顯示裝置的解析度(每英吋點)為何,影像的大小都會以英吋為單位正確。 例如,假設影像的圖元寬度為 216,而水平解析度為每英吋 72 點。 如果您呼叫此方法,在解析度為每英吋 96 點的裝置上繪製該影像,轉譯影像的圖元寬度會是 (216/72)*96 = 288。

另請參閱

適用於

.NET 9 及其他版本
產品 版本
.NET 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

DrawImage(Image, PointF[])

來源:
Graphics.cs
來源:
Graphics.cs
來源:
Graphics.cs
來源:
Graphics.cs
來源:
Graphics.cs

在指定的位置繪製指定的 Image,並具有指定的圖案和大小。

C#
public void DrawImage (System.Drawing.Image image, System.Drawing.PointF[] destPoints);

參數

image
Image

要繪製的 Image

destPoints
PointF[]

定義平行投影之三個 PointF 結構的陣列。

例外狀況

image null

範例

下列程式代碼範例是專為搭配 Windows Forms 使用而設計,而且需要 PaintEventArgse,這是 Paint 事件處理程式的參數。 程式代碼會執行下列動作:

  • 從範例資料夾中的 JPEG 檔案 SampImag.jpg 建立映像。

  • 建立點,以定義用來繪製影像的平行投影。

  • 將影像繪製到畫面。

平行投影的位置會在畫面上找出影像,而原始影像的大小和平行投影的大小和形狀會決定繪製影像的縮放和剪下。

C#
private void DrawImageParaF(PaintEventArgs e)
{
             
    // Create image.
    Image newImage = Image.FromFile("SampImag.jpg");
             
    // Create parallelogram for drawing image.
    PointF ulCorner = new PointF(100.0F, 100.0F);
    PointF urCorner = new PointF(550.0F, 100.0F);
    PointF llCorner = new PointF(150.0F, 250.0F);
    PointF[] destPara = {ulCorner, urCorner, llCorner};
             
    // Draw image to screen.
    e.Graphics.DrawImage(newImage, destPara);
}

備註

destPoints 參數會指定平行投影的三點。 三個 PointF 結構代表平行投影的左上角、右上角和左下角。 第四個點是從前三個推斷出來的,以形成平行投影。

image 物件表示的影像會縮放和剪下,以符合 destPoints 參數所指定的平行投影圖形。

另請參閱

適用於

.NET 9 及其他版本
產品 版本
.NET 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

DrawImage(Image, Rectangle)

來源:
Graphics.cs
來源:
Graphics.cs
來源:
Graphics.cs
來源:
Graphics.cs
來源:
Graphics.cs

在指定的位置繪製指定的 Image,並具有指定的大小。

C#
public void DrawImage (System.Drawing.Image image, System.Drawing.Rectangle rect);

參數

image
Image

要繪製的 Image

rect
Rectangle

Rectangle 結構,指定繪製影像的位置和大小。

例外狀況

image null

範例

下列程式代碼範例是專為搭配 Windows Forms 使用而設計,而且需要 PaintEventArgse,這是 Paint 事件處理程式的參數。 程式代碼會執行下列動作:

  • 從範例資料夾中的 JPEG 檔案 SampImag.jpg 建立映像。

  • 建立要在其中繪製影像的矩形。

  • 將影像繪製到畫面。

矩形的位置會在畫面上找出影像,而原始影像的大小和矩形的大小會決定繪製影像的縮放比例。

C#
private void DrawImageRect(PaintEventArgs e)
{
             
    // Create image.
    Image newImage = Image.FromFile("SampImag.jpg");
             
    // Create rectangle for displaying image.
    Rectangle destRect = new Rectangle(100, 100, 450, 150);
             
    // Draw image to screen.
    e.Graphics.DrawImage(newImage, destRect);
}

備註

image 物件所代表的影像會縮放至 rect 矩形的維度。

另請參閱

適用於

.NET 9 及其他版本
產品 版本
.NET 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

DrawImage(Image, RectangleF)

來源:
Graphics.cs
來源:
Graphics.cs
來源:
Graphics.cs
來源:
Graphics.cs
來源:
Graphics.cs

在指定的位置繪製指定的 Image,並具有指定的大小。

C#
public void DrawImage (System.Drawing.Image image, System.Drawing.RectangleF rect);

參數

image
Image

要繪製的 Image

rect
RectangleF

RectangleF 結構,指定繪製影像的位置和大小。

例外狀況

image null

範例

下列程式代碼範例是專為搭配 Windows Forms 使用而設計,而且需要 PaintEventArgse,這是 Paint 事件處理程式的參數。 程式代碼會執行下列動作:

  • 從範例資料夾中的 JPEG 檔案 SampImag.jpg 建立映像。

  • 建立要在其中繪製影像的矩形。

  • 將影像繪製到畫面。

矩形的位置會在畫面上找出影像,而影像的原始大小和矩形的大小會決定繪製影像的縮放比例。

C#
public void DrawImageRectF(PaintEventArgs e)
{
             
    // Create image.
    Image newImage = Image.FromFile("SampImag.jpg");
             
    // Create rectangle for displaying image.
    RectangleF rect = new RectangleF(100.0F, 100.0F, 450.0F, 150.0F);
             
    // Draw image to screen.
    e.Graphics.DrawImage(newImage, rect);
}

備註

image 物件所代表的影像會縮放至 rect 矩形的維度。

另請參閱

適用於

.NET 9 及其他版本
產品 版本
.NET 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

DrawImage(Image, Int32, Int32, Rectangle, GraphicsUnit)

來源:
Graphics.cs
來源:
Graphics.cs
來源:
Graphics.cs
來源:
Graphics.cs
來源:
Graphics.cs

在指定的位置繪製影像的一部分。

C#
public void DrawImage (System.Drawing.Image image, int x, int y, System.Drawing.Rectangle srcRect, System.Drawing.GraphicsUnit srcUnit);

參數

image
Image

要繪製的 Image

x
Int32

繪製影像左上角的 X 座標。

y
Int32

繪製影像左上角的 Y 座標。

srcRect
Rectangle

Rectangle 結構,指定要繪製之 image 物件的部分。

srcUnit
GraphicsUnit

GraphicsUnit 列舉的成員,指定 srcRect 參數所使用的量值單位。

例外狀況

image null

範例

下列程式代碼範例是專為搭配 Windows Forms 使用而設計,而且需要 PaintEventArgse,這是 Paint 事件處理程式的參數。 程式代碼會執行下列動作:

  • 從範例資料夾中的 JPEG 檔案 SampImag.jpg 建立映像。

  • 建立要繪製影像左上角的座標。

  • 建立來源矩形,從中擷取影像的一部分。

  • 將來源矩形的測量單位設定為圖元。

  • 將影像繪製到畫面。

來源矩形的大小會決定未調整的原始影像繪製到畫面的哪個部分。

C#
public void DrawImage2IntRect(PaintEventArgs e)
{
             
    // Create image.
    Image newImage = Image.FromFile("SampImag.jpg");
             
    // Create coordinates for upper-left corner of image.
    int x = 100;
    int y = 100;
             
    // Create rectangle for source image.
    Rectangle srcRect = new Rectangle(50, 50, 150, 150);
    GraphicsUnit units = GraphicsUnit.Pixel;
             
    // Draw image to screen.
    e.Graphics.DrawImage(newImage, x, y, srcRect, units);
}

備註

Image 會儲存圖元寬度的值,以及水平解析度的值(每英吋點)。 以英吋為單位的影像實體寬度是像素寬度除以水平解析度。 例如,圖元寬度為 360 的影像,而水平解析度為每英吋 72 個點,其實體寬度為 5 英吋。 類似的備註適用於圖元高度和實體高度。

這個方法會使用其實體大小繪製影像的一部分,因此影像部分的大小會以英吋為單位,而不論顯示裝置的解析度(每英吋點數)為何。 例如,假設影像部分的圖元寬度為 216,而水平解析度為每英吋 72 個點。 如果您呼叫此方法,在解析度為每英吋 96 點的裝置上繪製該影像部分,轉譯影像部分的圖元寬度會是 (216/72)*96 = 288。

另請參閱

適用於

.NET 9 及其他版本
產品 版本
.NET 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

DrawImage(Image, Single, Single)

來源:
Graphics.cs
來源:
Graphics.cs
來源:
Graphics.cs
來源:
Graphics.cs
來源:
Graphics.cs

使用指定位置的原始實體大小,繪製指定的 Image

C#
public void DrawImage (System.Drawing.Image image, float x, float y);

參數

image
Image

要繪製的 Image

x
Single

繪製影像左上角的 X 座標。

y
Single

繪製影像左上角的 Y 座標。

例外狀況

image null

範例

下列程式代碼範例是專為搭配 Windows Forms 使用而設計,而且需要 PaintEventArgse,這是 Paint 事件處理程式的參數。 程式代碼會執行下列動作:

  • 從範例資料夾中的 JPEG 檔案 SampImag.jpg 建立映像。

  • 建立要繪製影像左上角之點的座標。

  • 將未調整的影像繪製到畫面。

C#
public void DrawImage2Float(PaintEventArgs e)
{
             
    // Create image.
    Image newImage = Image.FromFile("SampImag.jpg");
             
    // Create coordinates for upper-left corner of image.
    float x = 100.0F;
    float y = 100.0F;
             
    // Draw image to screen.
    e.Graphics.DrawImage(newImage, x, y);
}

備註

Image 會儲存圖元寬度的值,以及水平解析度的值(每英吋點)。 以英吋為單位的影像實體寬度是像素寬度除以水平解析度。 例如,圖元寬度為 216 的影像,而水平解析度為每英吋 72 個點,其實體寬度為 3 英吋。 類似的備註適用於圖元高度和實體高度。

這個方法會使用其實體大小繪製影像,因此不論顯示裝置的解析度(每英吋點)為何,影像的大小都會以英吋為單位正確。 例如,假設影像的圖元寬度為 216,而水平解析度為每英吋 72 點。 如果您呼叫此方法,在解析度為每英吋 96 點的裝置上繪製該影像,轉譯影像的圖元寬度會是 (216/72)*96 = 288。

另請參閱

適用於

.NET 9 及其他版本
產品 版本
.NET 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

DrawImage(Image, Point[], Rectangle, GraphicsUnit)

來源:
Graphics.cs
來源:
Graphics.cs
來源:
Graphics.cs
來源:
Graphics.cs
來源:
Graphics.cs

在指定的位置和具有指定大小的指定位置繪製指定 Image 的指定部分。

C#
public void DrawImage (System.Drawing.Image image, System.Drawing.Point[] destPoints, System.Drawing.Rectangle srcRect, System.Drawing.GraphicsUnit srcUnit);

參數

image
Image

要繪製的 Image

destPoints
Point[]

定義平行投影之三個 Point 結構的陣列。

srcRect
Rectangle

Rectangle 結構,指定要繪製之 image 物件的部分。

srcUnit
GraphicsUnit

GraphicsUnit 列舉的成員,指定 srcRect 參數所使用的量值單位。

例外狀況

image null

範例

下列程式代碼範例是專為搭配 Windows Forms 使用而設計,而且需要 PaintEventArgse,這是 Paint 事件處理程式的參數。 程式代碼會執行下列動作:

  • 從範例資料夾中的 JPEG 檔案 SampImag.jpg 建立映像。

  • 建立點,以定義用來繪製影像的平行投影。

  • 建立矩形以選取要繪製的影像部分。

  • 將圖形繪圖單位設定為圖元。

  • 將影像繪製到畫面。

平行投影的位置會在畫面上找出影像,而矩形的大小和平行投影的大小和形狀會決定繪製影像的縮放和剪下。

C#
private void DrawImageParaRect(PaintEventArgs e)
{
    // Create image.
    Image newImage = Image.FromFile("SampImag.jpg");
             
    // Create parallelogram for drawing image.
    Point ulCorner = new Point(100, 100);
    Point urCorner = new Point(325, 100);
    Point llCorner = new Point(150, 250);
    Point[] destPara = {ulCorner, urCorner, llCorner};
             
    // Create rectangle for source image.
    Rectangle srcRect = new Rectangle(50, 50, 150, 150);
    GraphicsUnit units = GraphicsUnit.Pixel;
             
    // Draw image to screen.
    e.Graphics.DrawImage(newImage, destPara, srcRect, units);
}

備註

destPoints 參數會指定平行投影的三點。 三個 Point 結構代表平行投影的左上角、右上角和左下角。 第四個點是從前三個推斷出來的,以形成平行投影。

srcRect 參數會指定要繪製之 image 物件的矩形部分。 此部分會縮放並剪下,以符合 destPoints 參數所指定的平行投影。

另請參閱

適用於

.NET 9 及其他版本
產品 版本
.NET 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

DrawImage(Image, PointF[], RectangleF, GraphicsUnit)

來源:
Graphics.cs
來源:
Graphics.cs
來源:
Graphics.cs
來源:
Graphics.cs
來源:
Graphics.cs

在指定的位置和具有指定大小的指定位置繪製指定 Image 的指定部分。

C#
public void DrawImage (System.Drawing.Image image, System.Drawing.PointF[] destPoints, System.Drawing.RectangleF srcRect, System.Drawing.GraphicsUnit srcUnit);

參數

image
Image

要繪製的 Image

destPoints
PointF[]

定義平行投影之三個 PointF 結構的陣列。

srcRect
RectangleF

RectangleF 結構,指定要繪製之 image 物件的部分。

srcUnit
GraphicsUnit

GraphicsUnit 列舉的成員,指定 srcRect 參數所使用的量值單位。

例外狀況

image null

範例

下列程式代碼範例是專為搭配 Windows Forms 使用而設計,而且需要 PaintEventArgse,這是 Paint 事件處理程式的參數。 程式代碼會執行下列動作:

  • 從範例資料夾中的 JPEG 檔案 SampImag.jpg 建立映像。

  • 建立點,以定義要在其中繪製影像的目的地平行投影。

  • 建立來源矩形,從中擷取影像的一部分。

  • 將來源矩形的測量單位設定為圖元。

  • 將影像繪製到畫面。

目的地平行投影的位置會在畫面上找出影像、來源矩形的大小和目的平行投影的大小和形狀,決定繪製影像的縮放和剪下,而矩形的大小會決定原始影像的繪製到畫面的哪個部分。

C#
private void DrawImageParaFRectF(PaintEventArgs e)
{
             
    // Create image.
    Image newImage = Image.FromFile("SampImag.jpg");
             
    // Create parallelogram for drawing image.
    PointF ulCorner = new PointF(100.0F, 100.0F);
    PointF urCorner = new PointF(550.0F, 100.0F);
    PointF llCorner = new PointF(150.0F, 250.0F);
    PointF[] destPara = {ulCorner, urCorner, llCorner};
             
    // Create rectangle for source image.
    RectangleF srcRect = new RectangleF(50.0F, 50.0F, 150.0F, 150.0F);
    GraphicsUnit units = GraphicsUnit.Pixel;
             
    // Draw image to screen.
    e.Graphics.DrawImage(newImage, destPara, srcRect, units);
}

備註

destPoints 參數會指定平行投影的三點。 三個 PointF 結構代表平行投影的左上角、右上角和左下角。 第四個點是從前三個推斷出來的,以形成平行投影。

srcRect 參數會指定要繪製之 image 物件的矩形部分。 此部分會縮放並剪下,以符合 destPoints 參數所指定的平行投影。

另請參閱

適用於

.NET 9 及其他版本
產品 版本
.NET 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

DrawImage(Image, Rectangle, Rectangle, GraphicsUnit)

來源:
Graphics.cs
來源:
Graphics.cs
來源:
Graphics.cs
來源:
Graphics.cs
來源:
Graphics.cs

在指定的位置和具有指定大小的指定位置繪製指定 Image 的指定部分。

C#
public void DrawImage (System.Drawing.Image image, System.Drawing.Rectangle destRect, System.Drawing.Rectangle srcRect, System.Drawing.GraphicsUnit srcUnit);

參數

image
Image

要繪製的 Image

destRect
Rectangle

Rectangle 結構,指定繪製影像的位置和大小。 影像會縮放以符合矩形。

srcRect
Rectangle

Rectangle 結構,指定要繪製之 image 物件的部分。

srcUnit
GraphicsUnit

GraphicsUnit 列舉的成員,指定 srcRect 參數所使用的量值單位。

例外狀況

image null

範例

下列程式代碼範例是專為搭配 Windows Forms 使用而設計,而且需要 PaintEventArgse,這是 Paint 事件處理程式的參數。 程式代碼會執行下列動作:

  • 從範例資料夾中的 JPEG 檔案 SampImag.jpg 建立映像。

  • 建立要在其中繪製影像的目的地矩形。

  • 建立來源矩形,從中擷取影像的一部分。

  • 將來源矩形的測量單位設定為圖元。

  • 將影像繪製到畫面。

目的矩形的位置會在畫面上找出影像、來源和目的矩形的大小決定繪製影像的縮放比例,而來源矩形的大小會決定原始影像繪製到畫面的哪個部分。

C#
private void DrawImageRectRect(PaintEventArgs e)
{
             
    // Create image.
    Image newImage = Image.FromFile("SampImag.jpg");
             
    // Create rectangle for displaying image.
    Rectangle destRect = new Rectangle(100, 100, 450, 150);
             
    // Create rectangle for source image.
    Rectangle srcRect = new Rectangle(50, 50, 150, 150);
    GraphicsUnit units = GraphicsUnit.Pixel;
             
    // Draw image to screen.
    e.Graphics.DrawImage(newImage, destRect, srcRect, units);
}

備註

srcRect 參數會指定要繪製之 image 物件的矩形部分。 此部分會縮放以符合 destRect 參數所指定的矩形。

另請參閱

適用於

.NET 9 及其他版本
產品 版本
.NET 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

DrawImage(Image, RectangleF, RectangleF, GraphicsUnit)

來源:
Graphics.cs
來源:
Graphics.cs
來源:
Graphics.cs
來源:
Graphics.cs
來源:
Graphics.cs

在指定的位置和具有指定大小的指定位置繪製指定 Image 的指定部分。

C#
public void DrawImage (System.Drawing.Image image, System.Drawing.RectangleF destRect, System.Drawing.RectangleF srcRect, System.Drawing.GraphicsUnit srcUnit);

參數

image
Image

要繪製的 Image

destRect
RectangleF

RectangleF 結構,指定繪製影像的位置和大小。 影像會縮放以符合矩形。

srcRect
RectangleF

RectangleF 結構,指定要繪製之 image 物件的部分。

srcUnit
GraphicsUnit

GraphicsUnit 列舉的成員,指定 srcRect 參數所使用的量值單位。

例外狀況

image null

範例

下列程式代碼範例是專為搭配 Windows Forms 使用而設計,而且需要 PaintEventArgse,這是 Paint 事件處理程式的參數。 程式代碼會執行下列動作:

  • 從範例資料夾中的 JPEG 檔案 SampImag.jpg 建立映像。

  • 建立要在其中繪製影像的目的地矩形。

  • 建立來源矩形,從中擷取影像的一部分。

  • 將來源矩形的測量單位設定為圖元。

  • 將影像繪製到畫面。

目的矩形的位置會在畫面上找出影像、來源和目的矩形的大小決定繪製影像的縮放比例,而來源矩形的大小會決定原始影像繪製到畫面的哪個部分。

C#
public void DrawImageRectFRectF(PaintEventArgs e)
{
             
    // Create image.
    Image newImage = Image.FromFile("SampImag.jpg");
             
    // Create rectangle for displaying image.
    RectangleF destRect = new RectangleF(100.0F, 100.0F, 450.0F, 150.0F);
             
    // Create rectangle for source image.
    RectangleF srcRect = new RectangleF(50.0F, 50.0F, 150.0F, 150.0F);
    GraphicsUnit units = GraphicsUnit.Pixel;
             
    // Draw image to screen.
    e.Graphics.DrawImage(newImage, destRect, srcRect, units);
}

備註

srcRect 參數會指定要繪製之 image 物件的矩形部分。 此部分會縮放以符合 destRect 參數所指定的矩形。

另請參閱

適用於

.NET 9 及其他版本
產品 版本
.NET 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

DrawImage(Image, Int32, Int32)

來源:
Graphics.cs
來源:
Graphics.cs
來源:
Graphics.cs
來源:
Graphics.cs
來源:
Graphics.cs

使用原始的實體大小,在座標組所指定的位置繪製指定的影像。

C#
public void DrawImage (System.Drawing.Image image, int x, int y);

參數

image
Image

要繪製的 Image

x
Int32

繪製影像左上角的 X 座標。

y
Int32

繪製影像左上角的 Y 座標。

例外狀況

image null

範例

下列程式代碼範例是專為搭配 Windows Forms 使用而設計,而且需要 PaintEventArgse,這是 Paint 事件處理程式的參數。 程式代碼會執行下列動作:

  • 從範例資料夾中 SampImag.jpg 範例資料夾中的 JPEG 檔案 SampImag.jpg 建立映像。

  • 建立要繪製影像左上角之點的座標。

  • 繪製未調整的影像。

C#
public void DrawImage2Int(PaintEventArgs e)
{
                
    // Create image.
    Image newImage = Image.FromFile("SampImag.jpg");
                
    // Create coordinates for upper-left corner of image.
    int x = 100;
    int y = 100;
                
    // Draw image to screen.
    e.Graphics.DrawImage(newImage, x, y);
}

備註

Image 會儲存圖元寬度的值,以及水平解析度的值(每英吋點)。 以英吋為單位的影像實體寬度是像素寬度除以水平解析度。 例如,圖元寬度為 216 的影像,而水平解析度為每英吋 72 個點,其實體寬度為 3 英吋。 類似的備註適用於圖元高度和實體高度。

DrawImage 方法會使用其實體大小來繪製影像,因此不論顯示裝置的解析度(每英吋點數)為何,影像的大小都會以英吋為單位正確。 例如,假設影像的圖元寬度為 216,而水平解析度為每英吋 72 點。 如果您呼叫 DrawImage,在解析度為每英吋 96 點的裝置上繪製該影像,轉譯影像的圖元寬度會是 (216/72)*96 = 288。

另請參閱

適用於

.NET 9 及其他版本
產品 版本
.NET 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

DrawImage(Image, Point[], Rectangle, GraphicsUnit, ImageAttributes)

來源:
Graphics.cs
來源:
Graphics.cs
來源:
Graphics.cs
來源:
Graphics.cs
來源:
Graphics.cs

在指定的位置繪製指定 Image 的指定部分。

C#
public void DrawImage (System.Drawing.Image image, System.Drawing.Point[] destPoints, System.Drawing.Rectangle srcRect, System.Drawing.GraphicsUnit srcUnit, System.Drawing.Imaging.ImageAttributes? imageAttr);
C#
public void DrawImage (System.Drawing.Image image, System.Drawing.Point[] destPoints, System.Drawing.Rectangle srcRect, System.Drawing.GraphicsUnit srcUnit, System.Drawing.Imaging.ImageAttributes imageAttr);

參數

image
Image

要繪製的 Image

destPoints
Point[]

定義平行投影之三個 Point 結構的陣列。

srcRect
Rectangle

Rectangle 結構,指定要繪製之 image 物件的部分。

srcUnit
GraphicsUnit

GraphicsUnit 列舉的成員,指定 srcRect 參數所使用的量值單位。

imageAttr
ImageAttributes

ImageAttributes,指定 image 物件的重新著色和伽瑪資訊。

例外狀況

image null

範例

下列程式代碼範例是專為搭配 Windows Forms 使用而設計,而且需要 PaintEventArgse,這是 Paint 事件處理程式的參數。 程式代碼會執行下列動作:

  • 從範例資料夾中的 JPEG 檔案 SampImag.jpg 建立映像。

  • 建立點,以定義用來繪製影像的平行投影。

  • 建立矩形以選取要繪製的影像部分。

  • 將圖形繪圖單位設定為圖元。

  • 將原始影像繪製到畫面。

  • 建立額外的平行投影,在其中繪製調整后的影像。

  • 建立並設定調整影像的屬性,以具有大於一般伽瑪值。

  • 將調整后的影像繪製到畫面。

針對原始、未調整的平行投影,位置會在畫面上找出影像,而矩形的大小以及平行投影的大小和形狀會決定繪製影像的縮放和剪下。

C#
private void DrawImageParaRectAttrib(PaintEventArgs e)
{
    // Create image.
    Image newImage = Image.FromFile("SampImag.jpg");
             
    // Create parallelogram for drawing image.
    Point ulCorner1 = new Point(100, 100);
    Point urCorner1 = new Point(325, 100);
    Point llCorner1 = new Point(150, 250);
    Point[] destPara1 = {ulCorner1, urCorner1, llCorner1};
             
    // Create rectangle for source image.
    Rectangle srcRect = new Rectangle(50, 50, 150, 150);
    GraphicsUnit units = GraphicsUnit.Pixel;
             
    // Draw original image to screen.
    e.Graphics.DrawImage(newImage, destPara1, srcRect, units);
             
    // Create parallelogram for drawing adjusted image.
    Point ulCorner2 = new Point(325, 100);
    Point urCorner2 = new Point(550, 100);
    Point llCorner2 = new Point(375, 250);
    Point[] destPara2 = {ulCorner2, urCorner2, llCorner2};
             
    // Create image attributes and set large gamma.
    ImageAttributes imageAttr = new ImageAttributes();
    imageAttr.SetGamma(4.0F);
             
    // Draw adjusted image to screen.
    e.Graphics.DrawImage(newImage, destPara2, srcRect, units, imageAttr);
}

備註

destPoints 參數會指定平行投影的三點。 三個 Point 結構代表平行投影的左上角、右上角和左下角。 第四個點是從前三個推斷出來的,以形成平行投影。

srcRect 參數會指定要繪製之image 物件的矩形部分。 此部分會縮放並剪下,以符合 destPoints 參數所指定的平行投影。

另請參閱

適用於

.NET 9 及其他版本
產品 版本
.NET 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9