Create New Bitmap From Image : Bitmap « 2D Graphics « C# / C Sharp






Create New Bitmap From Image

       

//--------------------------------------------------------------------------
// 
//  Copyright (c) Microsoft Corporation.  All rights reserved. 
// 
//  File: Utilities.cs
//
//--------------------------------------------------------------------------

using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace ParallelMorph
{
    /// <summary>Helper utilities.</summary>
    public static class Utilities
    {

        /// <summary>Clones an image into a new Bitmap.</summary>
        /// <param name="source">The source image.</param>
        /// <returns>The new Bitmap.</returns>
        public static Bitmap CreateNewBitmapFrom(Image source) 
        { 
            return CreateNewBitmapFrom(source, 1); 
        }

        /// <summary>Creates a copy of the source image by scaling it with the specified scale value.</summary>
        /// <param name="source">The source image.</param>
        /// <param name="scalingFactor">The scaling factor to use when generating the target image.</param>
        /// <returns>The new Bitmap.</returns>
        public static Bitmap CreateNewBitmapFrom(Image source, float scalingFactor)
        {
            return CreateNewBitmapFrom(source, (int)(source.Width*scalingFactor), (int)(source.Height*scalingFactor));
        }

        /// <summary>Creates a copy of the source image using the specified target width and height.</summary>
        /// <param name="source">The source image.</param>
        /// <param name="targetWidth">The target width for the generated image.</param>
        /// <param name="targetHeight">The target height for the generated image.</param>
        /// <returns>The new Bitmap.</returns>
        public static Bitmap CreateNewBitmapFrom(Image source, int targetWidth, int targetHeight)
        {
            var newBmp = new Bitmap(targetWidth, targetHeight, PixelFormat.Format32bppArgb);
            using (var g = Graphics.FromImage(newBmp)) g.DrawImage(source, 0, 0, newBmp.Width, newBmp.Height);
            return newBmp;
        }

        /// <summary>Retrieves an element from a Tuple by item number.</summary>
        /// <typeparam name="T">Specifies the type of data contained in the tuple.</typeparam>
        /// <param name="tuple">The tuple.</param>
        /// <param name="itemNumber">The item number.</param>
        /// <returns>Item1 if itemNumber is 0, otherwise Item2 if itemNumber is 1.</returns>
        public static T Item<T>(this Tuple<T, T> tuple, int itemNumber)
        {
            switch (itemNumber)
            {
                case 0: return tuple.Item1;
                case 1: return tuple.Item2;
                default: throw new ArgumentOutOfRangeException("itemNumber");
            }
        }

    }
}

   
    
    
    
    
    
    
  








Related examples in the same category

1.Draw on an Bitmap
2.Resize the Bitmap using the lowest quality interpolation mode
3.Resize the Bitmap using the highest quality interpolation mode
4.Create Graphics object From Image
5.Bitmap.SetResolution
6.Bitmap property: Height, Physical Dimension, width, raw format and sizeBitmap property: Height, Physical Dimension, width, raw format and size
7.Create your own BitMapCreate your own BitMap
8.Draw shapes to the bitmap in memoryDraw shapes to the bitmap in memory
9.new Bitmap(bitmap, size)
10.Read Bitmap Size by using BinaryReader
11.Bitmap.HorizontalResolution
12.Use a color matrix to change the color properties of the image
13.Create a Bitmap image in memory and set its CompositingMode
14.Create a red color with an alpha component then draw a red circle to the bitmap in memory
15.Create a green color with an alpha component then draw a green rectangle to the bitmap in memory
16.write the pixel information to the console window
17.Double buffer with Bitmap
18.Draw an array of imagesDraw an array of images
19.Bit operation with PixelFormat.Alpha
20.PixelFormat.DontCare
21.Scale Bitmap By Percent
22.Draws Bitmap in a cell within a DataGridView
23.Bitmap Image Utils
24.Bitmap Operations
25.Make Bitmap from UIElement
26.Creates a new bitmap from a specific region of another bitmap
27.Allows drawing fonts with borders and auto centers the font on a bitmap.
28.Simple Resize Bmp
29.Create Thumbnail
30.Calculate the RBG projection
31.Make Thumb