private static byte[] RemoveAlphaLayerBytes(byte[] imageBytes, System.Drawing.Imaging.PixelFormat pixelFormat) { // Check if has a transparency layer if (pixelFormat.ToString().ToLower().EndsWith("argb")) { // Skip last byte (padding) - contents may vary per platform return(imageBytes.Where((b, i) => i % 4 != 3).ToArray()); } return(imageBytes); }
public static PixelFormat GetImagePixelFormat(System.Drawing.Imaging.PixelFormat pixelformat) { if (BitConverter.IsLittleEndian == false) { throw new NotSupportedException("Bigendian"); } switch (pixelformat) { case System.Drawing.Imaging.PixelFormat.Format24bppRgb: return(PixelFormat.Bgr); case System.Drawing.Imaging.PixelFormat.Format32bppArgb: return(PixelFormat.Bgra); default: throw new NotSupportedException(pixelformat.ToString()); } }
public static int GetComponentCount(this System.Drawing.Imaging.PixelFormat pixelFormat) { switch (pixelFormat) { case System.Drawing.Imaging.PixelFormat.Alpha: case System.Drawing.Imaging.PixelFormat.PAlpha: case System.Drawing.Imaging.PixelFormat.Format8bppIndexed: return(1); case System.Drawing.Imaging.PixelFormat.Format16bppGrayScale: case System.Drawing.Imaging.PixelFormat.Format16bppRgb555: case System.Drawing.Imaging.PixelFormat.Format16bppRgb565: case System.Drawing.Imaging.PixelFormat.Format16bppArgb1555: return(2); case System.Drawing.Imaging.PixelFormat.Format24bppRgb: return(3); case System.Drawing.Imaging.PixelFormat.Canonical: case System.Drawing.Imaging.PixelFormat.Format32bppArgb: case System.Drawing.Imaging.PixelFormat.Format32bppPArgb: case System.Drawing.Imaging.PixelFormat.Format32bppRgb: return(4); case System.Drawing.Imaging.PixelFormat.Format48bppRgb: return(6); case System.Drawing.Imaging.PixelFormat.Format64bppArgb: case System.Drawing.Imaging.PixelFormat.Format64bppPArgb: return(8); case System.Drawing.Imaging.PixelFormat.Format1bppIndexed: case System.Drawing.Imaging.PixelFormat.Extended: case System.Drawing.Imaging.PixelFormat.Format4bppIndexed: case System.Drawing.Imaging.PixelFormat.Gdi: case System.Drawing.Imaging.PixelFormat.Indexed: case System.Drawing.Imaging.PixelFormat.Max: case System.Drawing.Imaging.PixelFormat.Undefined: default: throw new InvalidEnumArgumentException(pixelFormat.ToString()); } }
private static PixelFormat MapPixelFormat(System.Drawing.Imaging.PixelFormat pixelFormat) { switch (pixelFormat) { case System.Drawing.Imaging.PixelFormat.Alpha: return(PixelFormat.Alpha); case System.Drawing.Imaging.PixelFormat.Canonical: return(PixelFormat.Bgra); case System.Drawing.Imaging.PixelFormat.Format24bppRgb: return(PixelFormat.Bgr); case System.Drawing.Imaging.PixelFormat.Format32bppPArgb: case System.Drawing.Imaging.PixelFormat.Format32bppArgb: return(PixelFormat.Bgra); case System.Drawing.Imaging.PixelFormat.Format48bppRgb: return(PixelFormat.Bgr); case System.Drawing.Imaging.PixelFormat.Format64bppPArgb: case System.Drawing.Imaging.PixelFormat.Format64bppArgb: return(PixelFormat.Bgra); default: throw new ArgumentException("Unsupported pixel format: " + pixelFormat.ToString()); } }
//------------------------------------------------------ // Helpers //------------------------------------------------------ private void getImageFormat(System.Drawing.Imaging.PixelFormat bitmap_format, ref PixelInternalFormat pif, ref PixelFormat pf, ref PixelType pt) { switch (bitmap_format) { case System.Drawing.Imaging.PixelFormat.Format8bppIndexed: // misses glColorTable setup pif = PixelInternalFormat.Rgb8; pf = PixelFormat.ColorIndex; pt = PixelType.Bitmap; break; case System.Drawing.Imaging.PixelFormat.Format16bppArgb1555: case System.Drawing.Imaging.PixelFormat.Format16bppRgb555: // does not work pif = PixelInternalFormat.Rgb5A1; pf = PixelFormat.Bgr; pt = PixelType.UnsignedShort5551Ext; break; //case System.Drawing.Imaging.PixelFormat.Format16bppRgb565: // pif = OpenTK.Graphics.OpenGL.PixelInternalFormat.R5G6B5IccSgix; // pf = OpenTK.Graphics.OpenGL.PixelFormat.R5G6B5IccSgix; // pt = OpenTK.Graphics.OpenGL.PixelType.UnsignedByte; // break; case System.Drawing.Imaging.PixelFormat.Format24bppRgb: // works pif = PixelInternalFormat.Rgb8; pf = PixelFormat.Bgr; pt = PixelType.UnsignedByte; break; case System.Drawing.Imaging.PixelFormat.Format32bppRgb: // has alpha too? wtf? case System.Drawing.Imaging.PixelFormat.Canonical: case System.Drawing.Imaging.PixelFormat.Format32bppArgb: // works pif = PixelInternalFormat.Rgba; pf = PixelFormat.Bgra; pt = PixelType.UnsignedByte; break; default: throw new Exception("Unsupported Pixel Format: " + bitmap_format.ToString()); } }
private void btn导入图像_Click(object sender, EventArgs e) { if (pictureBox1.Image == null) { return; } OpenFileDialog ofd = new OpenFileDialog(); if (!ofd.ShowDialog().Equals(DialogResult.OK)) { return; } Bitmap imb; try { imb = new Bitmap(ofd.FileName); if (imb.PixelFormat != culfmt) { throw new Exception("格式不符,输入图像必须是" + culfmt.ToString() + "\nFormat Error"); } if (imb.Width != w || imb.Height != h) { throw new Exception("大小不符" + "\nSize Error"); } } catch (Exception error) { MessageBox.Show(error.Message); return; } if (culfmt == System.Drawing.Imaging.PixelFormat.Format8bppIndexed) { save8Bit(imb); } }
/// <summary> /// Convert from PixelFormat to System.Windows.Media.PixelFormat /// </summary> /// <param name="pixelFormat"></param> /// <exception cref="NotSupportedException">Convertion is not available</exception> /// <returns></returns> public static System.Windows.Media.PixelFormat Convert(this PixelFormat pixelFormat) { if (pixelFormat == PixelFormat.Format16bppGrayScale) { return(PixelFormats.Gray16); } if (pixelFormat == PixelFormat.Format16bppRgb555) { return(PixelFormats.Bgr555); } if (pixelFormat == PixelFormat.Format16bppRgb565) { return(PixelFormats.Bgr565); } if (pixelFormat == PixelFormat.Indexed) { return(PixelFormats.Bgr101010); } if (pixelFormat == PixelFormat.Format1bppIndexed) { return(PixelFormats.Indexed1); } if (pixelFormat == PixelFormat.Format4bppIndexed) { return(PixelFormats.Indexed4); } if (pixelFormat == PixelFormat.Format8bppIndexed) { return(PixelFormats.Indexed8); } if (pixelFormat == PixelFormat.Format24bppRgb) { return(PixelFormats.Bgr24); } if (pixelFormat == PixelFormat.Format32bppArgb) { return(PixelFormats.Bgr32); } if (pixelFormat == PixelFormat.Format32bppPArgb) { return(PixelFormats.Pbgra32); } if (pixelFormat == PixelFormat.Format32bppRgb) { return(PixelFormats.Bgr32); } if (pixelFormat == PixelFormat.Format48bppRgb) { return(PixelFormats.Rgb48); } if (pixelFormat == PixelFormat.Format64bppArgb) { return(PixelFormats.Prgba64); } if (pixelFormat == PixelFormat.Undefined) { return(PixelFormats.Default); } throw new NotSupportedException("Convertion not supported with " + pixelFormat.ToString()); }