c#gdi+ image Processing

Source: Internet
Author: User
Tags transparent color scale image

Http://www.cnblogs.com/BLoodMaster/archive/2010/03/20/1690726.html

Supported formats: BMP, GIF, JPEG, EXIF, PNG, TIFF, ICON, WMF, EMF, etc., covering almost all common formats

Image class:

    1. The image class: Bitmap and Metafile classes provide an abstract base class for functionality.
    2. Metafile class: Defines a graphical metafile that contains records describing a series of graphical operations that can be recorded (constructed) and replayed (displayed)
    3. Bitmap class: Encapsulates a GDI + bitmap that consists of pixel data for a graphic image and its properties, and bitmap is an object used to process images defined by pixel data.

Common Properties and methods:

Name

Description

Public properties

Height

Gets the height of this image object.

Rawformat

Gets the format of this image object.

Size

Gets the width and height of this image object.

Width

Gets the width of this image object.

Public methods

GetPixel

Gets the color of the specified pixel in this bitmap.

Maketransparent

Makes the default transparent color transparent to this bitmap.

RotateFlip

Rotate, flip, or colleague rotate and flip an image object.

Save

Saves the image object to the specified stream object in the specified format.

SetPixel

Sets the color of the specified pixel in the bitmap object.

Setpropertyitem

Sets the specified property item to the specified value.

Setresolution

Sets the resolution for this bitmap.

To construct an bitmap instance:

Public Bitmap (Image); Create from an existing image, which can be replaced by equivalent parameters

public Bitmap (int, int);

Public Bitmap (Image, BOOL);//bool: Color correction flag bit

Public Bitmap (Type, string);//type the class that extracts the resource, string resource name

public Bitmap (int, int, pixelformat);//PixelFormat Pixel format Enumeration

public Bitmap (int, int, graphics);//Graphics Set resolution

public Bitmap (int, int, int stride, PixelFormat, INTPTR);//Stride Specifies an integer that is the byte offset between the beginning of the adjacent scan line. The value passed to this parameter must be a multiple of 4. IntPtr pointer to a byte array that contains the pixel data.

PixelFormat enumeration:

Name

Description

Dontcare = 0

No pixel format specified

Undefined = 0

Pixel format Not defined

Max = 15

The maximum value of this enumeration

Indexed = 65536

The pixel data contains the color index values, which means that the values are the indexes of the colors in the system color table, not the individual color values.

Gdi = 131072

Pixel data contains GDI colors

format16bpprgb555 = 135173

The specified format is 16 bits per pixel, and the red, green, and blue components each use 5 bits. The remaining 1 bits not used

format16bpprgb565 = 135174

The specified format is 16 bits per pixel, the red component uses 5 bits, the green component uses 6 bits, and the blue component uses 5 bits

Format24bpprgb = 137224

The specified format is 24 bits per pixel, and the red, green, and blue components each use 8 bits

Format32bpprgb = 139273

The specified format is 32 bits per pixel, and the red, green, and blue components each use 8 bits. The remaining 8 bits not used

format1bppindexed = 196865

Specifies that the pixel format is 1 bits per pixel and specifies that it uses indexed colors. So there are two colors in the color table

format4bppindexed = 197634

Specifies a 4-bit format per pixel and an index has been created

format8bppindexed = 198659

The specified format is 8 bits per pixel and an index has been created. So there are 256 colors in the color table

Alpha = 262144

The pixel data contains an alpha value that has not been made from the left multiply

format16bppargb1555 = 397319

The pixel format is 16 bits per pixel. This color information specifies 32,768 tones, 5 bits red, 5 bits green, 5 bits blue, and 1 bits is alpha.

Palpha = 524288

The pixel format contains the alpha value from the left multiply

Format32bpppargb = 925707

The specified format is 32 bits per pixel, and the alpha, red, green, and blue components each use 8 bits. The red, green, and blue components are left-multiplied according to the alpha component

Extended = 1048576

Keep

Format16bppgrayscale = 1052676

The pixel format is 16 bits per pixel. This color information specifies 65536 shades of gray

Format48bpprgb = 1060876

The specified format is 48 bits per pixel, and the red, green, and blue components each use 16 bits

Format64bpppargb = 1851406

The specified format is 64 bits per pixel, and the alpha, red, green, and blue components each use 16 bits. The red, green, and blue components are left-multiplied according to the alpha component

Canonical = 2097152

The default pixel format, 32 bits per pixel. This format specifies 24-bit color depth and a 8-bit alpha channel

Format32bppargb = 2498570

The specified format is 32 bits per pixel; alpha, red, green, and blue components each use 8 bits

Format64bppargb = 3424269

The specified format is 64 bits per pixel; alpha, red, green, and blue components each use 16 bits

Input to the image:

    1. The Image property setting in the Properties dialog box when the form is designed
    2. In-Program dialog box input

In-Program dialog box input: (example)

Open the dialog box:

OpenFileDialog Ofdlg = new OpenFileDialog ();

Ofdlg. Filter = "BMP File (*.bmp) |*.bmp";

if (Ofdlg. ShowDialog () = = DialogResult.OK)

{

Bitmap image = New Bitmap (Ofdlg. FileName);

}

Save dialog box:

String str;

Bitmap box1 = new Bitmap (picturebox1.image);

SaveFileDialog Sfdlg = new SaveFileDialog ();

Sfdlg. Filter = "BMP file (*. BMP) |*. bmp| All File (*. *) |*.* ";

Sfdlg. ShowDialog ();

str = Sfdlg. FileName;

Box1. Save (str);

For the use of OpenFileDialog and SaveFileDialog, refer to the MSDN related sections.

Conversion of image formats:

The second parameter in the Save method of the bitmap object specifies the format in which the image is saved. Modify this parameter to convert the image format.

Supported formats for bitmaps: Imaging.imageformat

Name

Description

Bmp

Gets the bitmap image format (BMP).

Emf

Gets the enhanced Windows Metafile image format (EMF).

Exif

Gets the exchangeable image file (Exif) format.

Gif

Gets the Graphics Interchange Format (GIF) image format.

Guid

Gets the GUID structure that represents this Imageforma object.

Icon

Gets the Windows Icon image format.

Jpeg

Gets the joint Image Expert Group (JPEG) image format.

Memorybmp

Gets the memory bitmap image format.

Png

Gets the web-Portable Network Graphics (PNG) image format.

Tiff

Gets the label image file format (TIFF) image format.

Wmf

Gets the Windows Metafile (WMF) image format.

Image copy and paste clipboard:

Common methods:

Name

Description

Clear

Removes all data from the Clipboard.

Containsdata

Indicates whether the Clipboard has data in the specified format, or can be converted to this format.

ContainsImage

Indicates whether there is a Bitmap format in the Clipboard or data that can be converted to this format.

ContainsText

is overloaded. Indicates whether text data exists in the Clipboard.

GetData

Retrieves the data in the specified format from the Clipboard.

GetDataObject

Retrieves data that is currently on the system Clipboard.

Getfiledroplist

Retrieves a collection of file names from the Clipboard.

GetImage

Retrieves the image on the Clipboard.

GetText

is overloaded. Retrieves text data from the Clipboard.

Setaudio

is overloaded. Adds data from the Waveaudio format to the Clipboard.

SetData

Adds data from the specified format to the Clipboard.

SetDataObject

is overloaded. Place the data on the system Clipboard.

SetImage

Adds an image of the bitmap format to the Clipboard.

SetText

is overloaded. Adds text data to the Clipboard.

Step: Place the data on the Clipboard. Retrieves data from the Clipboard.

    1. Place data on the Clipboard

You can place data on the Clipboard through the SetDataObject method, the SetDataObject method has the following three forms of definition:

public static void SetDataObject (object);

public static void SetDataObject (object, bool);//bool when the program exits, remains on the clipboard

public static void SetDataObject (object, bool, int, int);//number of times and milliseconds between attempts and two attempts

    1. Retrieving data from the Clipboard

public static IDataObject GetDataObject ();

Color processing of images:

Adjust resolution: The so-called resolution refers to the screen of the resolution, by how many pixels, the larger the value, the more clear the image.

Algorithm Description: Transform the image of the n*n resolution into a m*m resolution method is to divide the source image into n/m*n/m sub-image block, and then the n/m*n/m sub-image block of all the pixels of the color of f (i,j) to set the color value to achieve the purpose of reducing the resolution.

Adjust color:

Ashing: Algorithm Description: The color value of color image pixels is decomposed into three primary colors R, G, B, the average of the values, and then use the SetPixel method to generate an image with the average parameter.

Gray-scale image processing: According to the specific input and output gray-scale conversion relationship, enhance the image grayscale, processing the image of the medium gray value increases, the image becomes bright.

Color name

R value

G value

B Value

Red

255

0

0

Blue

0

0

255

Green

0

255

0

White

255

255

255

Yellow

255

255

0

Black

0

0

0

Green

0

255

255

Magenta

255

0

255

Color table: (RGB)

c#gdi+ image Processing

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.