using System; using System.Drawing; using System.Drawing.Imaging; class Example_SetColorDepth { public static void Main() { Bitmap myBitmap; ImageCodecInfo myImageCodecInfo; Encoder myEncoder; EncoderParameter myEncoderParameter; EncoderParameters myEncoderParameters; // Create a Bitmap object based on a BMP file. myBitmap = new Bitmap(@"C:\Documents and Settings\All Users\Documents\My Music\music.jpg"); // Get an ImageCodecInfo object that represents the TIFF codec. myImageCodecInfo = GetEncoderInfo("image/jpeg"); // Create an Encoder object based on the GUID // for the ColorDepth parameter category. myEncoder = Encoder.ColorDepth; // Create an EncoderParameters object. // An EncoderParameters object has an array of EncoderParameter // objects. In this case, there is only one // EncoderParameter object in the array. myEncoderParameters = new EncoderParameters(1); // Save the image with a color depth of 24 bits per pixel. myEncoderParameter = new EncoderParameter(myEncoder, 24L); myEncoderParameters.Param[0] = myEncoderParameter; myBitmap.Save("Shapes24bpp.jpeg", myImageCodecInfo, myEncoderParameters); } private static ImageCodecInfo GetEncoderInfo(String mimeType) { int j; ImageCodecInfo[] encoders; encoders = ImageCodecInfo.GetImageEncoders(); for(j = 0; j < encoders.Length; ++j) { if(encoders[j].MimeType == mimeType) return encoders[j]; } return null; } }
'API for bitmap get/setting array Private Declare Function GetBitmapBits Lib "gdi32" _ (ByVal hBitmap As Long, ByVal dwCount As Long, lpBits As Any) As Long Private Declare Function GetObject Lib "gdi32" Alias "GetObjectA" _ (ByVal hObject As Long, ByVal nCount As Long, lpObject As Any) As Long Private Declare Function SetBitmapBits Lib "gdi32" _ (ByVal hBitmap As Long, ByVal dwCount As Long, lpBits As Any) As Long Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" _ (Destination As Any, Source As Any, ByVal Length As Long) Private Type BITMAP '14 bytes bmType As Long bmWidth As Long bmHeight As Long bmWidthBytes As Long bmPlanes As Integer bmBitsPixel As Integer bmBits As Long End Type Private ImgHeader As BITMAP Private Sub Command1_Click() Picture1.Picture = LoadPicture("c:\emp4bit.bmp") GetObject Picture1.Image, Len(ImgHeader), ImgHeader MsgBox "Color depth = " & ImgHeader.bmBitsPixel & " bits" MsgBox "Bytes per row = " & ImgHeader.bmWidthBytes End Sub
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)