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

FromImage() public static method

public static FromImage ( Image image ) : Graphics
image Image
return Graphics
        public static Graphics FromImage(Image image)
        {
            if (image == null)
                throw new ArgumentNullException ("image");

            if ((image.PixelFormat & PixelFormat.Indexed) != 0)
                throw new Exception ("Cannot create Graphics from an indexed bitmap.");

            Bitmap b = image as Bitmap;
            if (b == null)
                throw new Exception ("Can not create Graphics contexts from " + image.GetType () + " Images, only Bitmaps are supported");

            var bitmapContext = b.GetRenderableContext ();

            return new Graphics (bitmapContext, false);
        }

Usage Example

Example #1
3
        /// <summary>
        /// Set the size of the back buffer to the size of the control
        /// </summary>
        private void InitBuffer()
        {
            Graphics = this.CreateGraphics();

            Rectangle disp = RealRectangle;
            BackBuffer = new Bitmap(disp.Width > 0 ? disp.Width : 1, disp.Height > 0 ? disp.Height : 1);
            BufferGraphics = Graphics.FromImage(BackBuffer);
            BufferGraphics.TextRenderingHint = Graphics.TextRenderingHint;
        }
All Usage Examples Of System.Drawing.Graphics::FromImage