System.Drawing.Graphics.DrawImage C# (CSharp) Method

DrawImage() public method

Draws the specified portion of the specified Image at the specified location and with the specified size. The destPoints specifies a parallelogram with the first point specifying the upper left corner, second point specifying the upper right corner and the third point specifying the lower left corner. The srcRect parameter specifies a rectangular portion of the image object to draw. This portion is scaled up or down (in the case where source rectangle overruns the bounds of the image) to fit inside the rectangle specified by the destRect parameter.
public DrawImage ( Image image, PointF destPoints, RectangleF srcRect, GraphicsUnit srcUnit ) : void
image Image Image.
destPoints PointF Destination points.
srcRect RectangleF Source rect.
srcUnit GraphicsUnit Source unit.
return void
        public void DrawImage(Image image, PointF [] destPoints, RectangleF srcRect, GraphicsUnit srcUnit)
        {
            if (image == null)
                throw new ArgumentNullException ("image");
            if (destPoints == null)
                throw new ArgumentNullException ("destPoints");

            if (destPoints.Length < 3)
                throw new ArgumentException ("Destination points must be an array with a length of 3 or 4. " +
                                             "A length of 3 defines a parallelogram with the upper-left, upper-right, " +
                                             "and lower-left corners. A length of 4 defines a quadrilateral with the " +
                                             "fourth element of the array specifying the lower-right coordinate.");

            // Windows throws a Not Implemented error if the points are more than 3
            if (destPoints.Length > 3)
                throw new NotImplementedException ();

            var srcRect1 = srcRect;

            // If the source units are not the same we need to convert them
            // The reason we check for Pixel here is that our graphics already has the Pixel's baked into the model view transform
            if (srcUnit != graphicsUnit && srcUnit != GraphicsUnit.Pixel)
            {
                ConversionHelpers.GraphicsUnitConversion (srcUnit, graphicsUnit, image.HorizontalResolution, image.VerticalResolution,  ref srcRect1);
            }

            // Obtain the subImage
            var subImage = image.NativeCGImage.WithImageInRect (srcRect1.ToCGRect ());

            // If we do not have anything to draw then we exit here
            if (subImage.Width == 0 || subImage.Height == 0)
                return;

            // create our rectangle.  Offset is 0 because the CreateGeometricTransform bakes our x,y offset in there.
            var rect = new RectangleF (0,0, destPoints [1].X - destPoints [0].X, destPoints [2].Y - destPoints [0].Y);

            // We need to flip our Y axis so the image appears right side up
            var geoTransform = new CGAffineTransform (1, 0, 0, -1, 0, rect.Height);

            // Make sure we scale the image in case the source rectangle
            // overruns our subimage bounds (width and/or height)
            float scaleX = subImage.Width/srcRect1.Width;
            float scaleY = subImage.Height/srcRect1.Height;
            geoTransform.Scale (scaleX, scaleY);

            //var geott = GeomUtilities.CreateGeometricTransform (rect, destPoints);
            geoTransform.Multiply (GeomUtilities.CreateGeometricTransform (rect, destPoints));

            // Apply our transform to the context
            context.ConcatCTM (geoTransform);

            // now we draw our image.
            context.DrawImage(rect.ToCGRect (), subImage);

            // Now we revert our image transform from the context
            var revert = CGAffineTransform.CGAffineTransformInvert (geoTransform);
            context.ConcatCTM (revert);
        }

Same methods

Graphics::DrawImage ( Image image, Point destPoints ) : void
Graphics::DrawImage ( Image image, Point destPoints, Rectangle srcRect, GraphicsUnit srcUnit ) : void
Graphics::DrawImage ( Image image, Point destPoints, Rectangle srcRect, GraphicsUnit srcUnit, ImageAttributes imageAttr ) : void
Graphics::DrawImage ( Image image, Point destPoints, Rectangle srcRect, GraphicsUnit srcUnit, ImageAttributes imageAttr, DrawImageAbort callback ) : void
Graphics::DrawImage ( Image image, Point destPoints, Rectangle srcRect, GraphicsUnit srcUnit, ImageAttributes imageAttr, DrawImageAbort callback, int callbackData ) : void
Graphics::DrawImage ( Image image, PointF point ) : void
Graphics::DrawImage ( Image image, PointF destPoints, RectangleF srcRect, GraphicsUnit srcUnit, ImageAttributes imageAttr ) : void
Graphics::DrawImage ( Image image, PointF destPoints, RectangleF srcRect, GraphicsUnit srcUnit, ImageAttributes imageAttr, DrawImageAbort callback ) : void
Graphics::DrawImage ( Image image, PointF destPoints, RectangleF srcRect, GraphicsUnit srcUnit, ImageAttributes imageAttr, DrawImageAbort callback, int callbackData ) : void
Graphics::DrawImage ( Image image, Rectangle rect ) : void
Graphics::DrawImage ( Image image, Rectangle destRect, Rectangle srcRect, GraphicsUnit srcUnit ) : void
Graphics::DrawImage ( Image image, Rectangle destRect, float srcX, float srcY, float srcWidth, float srcHeight, GraphicsUnit srcUnit ) : void
Graphics::DrawImage ( Image image, Rectangle destRect, float srcX, float srcY, float srcWidth, float srcHeight, GraphicsUnit srcUnit, ImageAttributes imageAttrs ) : void
Graphics::DrawImage ( Image image, Rectangle destRect, float srcX, float srcY, float srcWidth, float srcHeight, GraphicsUnit srcUnit, ImageAttributes imageAttrs, DrawImageAbort callback ) : void
Graphics::DrawImage ( Image image, Rectangle destRect, float srcX, float srcY, float srcWidth, float srcHeight, GraphicsUnit srcUnit, ImageAttributes imageAttrs, DrawImageAbort callback, IntPtr callbackData ) : void
Graphics::DrawImage ( Image image, Rectangle destRect, int srcX, int srcY, int srcWidth, int srcHeight, GraphicsUnit srcUnit ) : void
Graphics::DrawImage ( Image image, Rectangle destRect, int srcX, int srcY, int srcWidth, int srcHeight, GraphicsUnit srcUnit, ImageAttributes imageAttr ) : void
Graphics::DrawImage ( Image image, Rectangle destRect, int srcX, int srcY, int srcWidth, int srcHeight, GraphicsUnit srcUnit, ImageAttributes imageAttr, DrawImageAbort callback ) : void
Graphics::DrawImage ( Image image, Rectangle destRect, int srcX, int srcY, int srcWidth, int srcHeight, GraphicsUnit srcUnit, ImageAttributes imageAttrs, DrawImageAbort callback, IntPtr callbackData ) : void
Graphics::DrawImage ( Image image, RectangleF rect ) : void
Graphics::DrawImage ( Image image, RectangleF destRect, RectangleF srcRect, GraphicsUnit srcUnit ) : void
Graphics::DrawImage ( Image image, float x, float y ) : void
Graphics::DrawImage ( Image image, float x, float y, RectangleF srcRect, GraphicsUnit srcUnit ) : void
Graphics::DrawImage ( Image image, float x, float y, float width, float height ) : void
Graphics::DrawImage ( Image image, int x, int y ) : void
Graphics::DrawImage ( Image image, int x, int y, Rectangle srcRect, GraphicsUnit srcUnit ) : void
Graphics::DrawImage ( Image image, int x, int y, int width, int height ) : void
Graphics::DrawImage ( RectangleF rect, CGImage image, CGAffineTransform transform ) : void

Usage Example

Example #1
0
 public void _drawTabHeader(Graphics nGraphics, Font nFont, Rectangle nRectangle)
 {
     int width_ = nRectangle.Height - 2;
     switch (mSideTabStatus)
     {
         case SideTabStatus_.mNormal_:
             ControlPaint.DrawBorder3D(nGraphics, nRectangle, Border3DStyle.RaisedInner);
             nGraphics.DrawImage(mSideTabImage, nRectangle.X + 1, nRectangle.Y + 1, width_, width_);
             nGraphics.DrawString(mSideTabName, nFont, SystemBrushes.ControlText, new PointF(nRectangle.X + width_ + 2, nRectangle.Y + 2));
             break;
         case SideTabStatus_.mSelected_:
             ControlPaint.DrawBorder3D(nGraphics, nRectangle, Border3DStyle.Sunken);
             nGraphics.DrawImage(mSideTabImage, nRectangle.X + 1, nRectangle.Y + 1, width_, width_);
             nGraphics.DrawString(mSideTabName, nFont, SystemBrushes.ControlText, new PointF(nRectangle.X + width_ + 2, nRectangle.Y + 2));
             break;
         case SideTabStatus_.mDragged_:
             ControlPaint.DrawBorder3D(nGraphics, nRectangle, Border3DStyle.RaisedInner);
             nRectangle.X += 1;
             nRectangle.Y += 1;
             nRectangle.Width -= 2;
             nRectangle.Height -= 2;
             nGraphics.FillRectangle(SystemBrushes.ControlDarkDark, nRectangle);
             nGraphics.DrawString(mSideTabName, nFont, SystemBrushes.HighlightText, new PointF(nRectangle.X + width_ , nRectangle.Y));
             break;
     }
 }
All Usage Examples Of System.Drawing.Graphics::DrawImage