C # How to obtain BMP bitmap in image processing,

Source: Internet
Author: User
Tags bmp image

C # How to obtain BMP bitmap in image processing,

Today, when I wrote an image processing host computer, I saw a good program and posted it here for analysis and learning.

The first step is to obtain the BMP bitmap.

OpenFileDialog openFileDialog = new OpenFileDialog ();

// Assume that you have changed the directory during file search. If the dialog box restores the current directory to its initial value, the value is true. Otherwise, the value is false. The default value is false. That is, to remember the last selected folder

OpenFileDialog. RestoreDirectory = true;

If (openFileDialog. ShowDialog () = DialogResult. OK)

{

Bitmap Temp = (Bitmap) Bitmap. FromFile (openFileDialog. FileName); // convert an image of any format to a BMP Image

If (IsGrayBitmap (Temp) = true) // determines whether the image is a gray image.

SrcBmp = Temp;

Else

{

SrcBmp = ConvertToGrayBitmap (Temp); // convert a color image to a grayscale image

Temp. Dispose (); // release the resources occupied by the image

}

/***************************** Determine whether a gray image is ******* ************************/

Private bool IsGrayBitmap (Bitmap Bmp)

{

If (Bmp. PixelFormat! = PixelFormat. Format8bppIndexed) // first, determine whether the pixel is 8-bit and 255-depth.

{

Return false;

}

If (Bmp. Palette. Entries. Length! = 256 )//

{

Return false;

}

For (int Y = 0; Y <Bmp. Palette. Entries. Length; Y ++)

{

If (Bmp. Palette. Entries [Y]! = Color. FromArgb (255, Y ))

{

Return false;

}

}

Return true;

}

/**************************** Create a grayscale image ******* ***********************/

Private Bitmap CreateGrayBitmap (int Width, int Height)

{

// Create an 8-bit gray image

Bitmap Bmp = new Bitmap (Width, Height, PixelFormat. Format8bppIndexed );

ColorPalette Pal = Bmp. Palette;

For (int Y = 0; Y <Pal. Entries. Length; Y ++)

{

Pal. Entries [Y] = Color. FromArgb (255, Y); // converts RGB to a gray Color palette.

}

Bmp. Palette = Pal;

Return Bmp;

}

/**************************** Create a grayscale image ******* ***********************/

Private Bitmap ConvertToGrayBitmap (Bitmap Src)

{

Bitmap Dest = CreateGrayBitmap (Src. Width, Src. Height );

BitmapData SrcData = Src. lockBits (new Rectangle (0, 0, Src. width, Src. height), ImageLockMode. readWrite, PixelFormat. format24bppRgb); // I have not understood the specific role

BitmapData DestData = Dest. LockBits (new Rectangle (0, 0, Dest. Width, Dest. Height), ImageLockMode. ReadWrite, Dest. PixelFormat );

Int Width = SrcData. Width, Height = SrcData. Height;

Int SrcStride = SrcData. Stride, DestStride = DestData. Stride;

Byte * SrcP, DestP; // c # The unsafe function must be enabled when the pointer is used. The following describes how to enable the unsafe function.

For (int Y = 0; Y <Height; Y ++)

{

SrcP = (byte *) SrcData. Scan0 + Y * SrcStride; // The unsafe function must be enabled somewhere. In fact, the unsafe function in C # is very safe, which is very scary.

DestP = (byte *) DestData. Scan0 + Y * DestStride;

For (int X = 0; X <Width; X ++)

{

DestP = (byte) (* SrcP + (SrcP + 1) <1) + * (SrcP + 2)> 2 ); // formula for converting a color image into a gray image

SrcP + = 3;

DestP ++;

}

}

Src. UnlockBits (SrcData );

Dest. UnlockBits (DestData); // unlock

Return Dest;

}

Then, when the class is declared, it must be declared as unsafe.

Public unsafe partial class Form1: Form

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.