public void SaveAdd() { string filename = @"saveadd.tif"; FreeImageBitmap fib = new FreeImageBitmap(100, 100, PixelFormat.Format24bppRgb); try { fib.SaveAdd(); Assert.Fail(); } catch { } Assert.IsFalse(File.Exists(filename)); fib.Save(filename); fib.AdjustBrightness(0.3d); fib.SaveAdd(); FreeImageBitmap other = new FreeImageBitmap(100, 100, PixelFormat.Format24bppRgb); foreach (Scanline <RGBTRIPLE> scanline in other) { for (int i = 0; i < scanline.Length; i++) { scanline[i] = new RGBTRIPLE(Color.FromArgb(0x339955)); } } fib.SaveAdd(other); other.SaveAdd(filename); other.Dispose(); fib.Dispose(); fib = new FreeImageBitmap(filename); Assert.AreEqual(4, fib.FrameCount); fib.Dispose(); File.Delete(filename); Assert.IsFalse(File.Exists(filename)); }
public void PopulateWithData(byte[] data, int StartAddress) { bmciHeader = new BITMAPCOREHEADER(); bmciHeader.PopulateWithData(data, StartAddress + 0); bmciColors = new RGBTRIPLE(); bmciColors.PopulateWithData(data, StartAddress + 12); }
private RGBTRIPLE ReadPixel( ) { RGBTRIPLE x = new RGBTRIPLE(); int c = Marshal.SizeOf(x); byte [] buffer = new byte[c]; stream.Read(buffer, 0, c); unsafe { fixed(byte *p = buffer) { x = *((RGBTRIPLE *)p); } } return(x); }
private void ReadAndShowBitmap(Graphics graph) { stream = new FileStream(fileName, FileMode.Open, FileAccess.Read); BITMAPFILEHEADER fileHeader = ReadFileHeader_UseBinaryReader(); //文件头 //BITMAPFILEHEADER fileHeader = ReadFileHeader(); //文件头 BITMAPINFOHEADER bmpInfo = ReadInfo(); //信息头 if (bmpInfo.biBitCount != 24) { MessageBox.Show("本程序只能处理24位位图"); stream.Close(); return; } int w = bmpInfo.biWidth; int h = bmpInfo.biHeight; stream.Seek(fileHeader.bfOffBits, SeekOrigin.Begin); //略去一些字节,定位到图像数据处 int LineSize = w * 3; if (LineSize % 4 != 0) { LineSize += 4 - (LineSize % 4); //每个图像点占3个字节。若一行不是4的倍数,则补齐 } for (int i = 1; i <= h; i++) //各行 { for (int j = 1; j <= w; j++) //各点 { RGBTRIPLE point = ReadPixel(); Color color = Color.FromArgb(point.rgbtRed, point.rgbtGreen, point.rgbtBlue); graph.DrawLine(new Pen(color, 1), j, h - i, j, h - i + 1); //注意图像的坐标上下是反的,另外,线的长度为1,才能画出点来 } if (w * 3 % 4 != 0) //每行不是4的倍数,略出空字节 { //stream.Seek( fileHeader.bfOffBits + LineSize * i, SeekOrigin.Begin ); stream.Seek(4 - w * 3 % 4, SeekOrigin.Current); } } stream.Close(); }
public void RGBTRIPLE() { RGBTRIPLE rgbt = new RGBTRIPLE(); Assert.AreEqual(0, rgbt.rgbtBlue); Assert.AreEqual(0, rgbt.rgbtGreen); Assert.AreEqual(0, rgbt.rgbtRed); rgbt = new RGBTRIPLE(Color.Chartreuse); Assert.That(EqualColors(Color.Chartreuse, rgbt.Color)); rgbt = new RGBTRIPLE(Color.FromArgb(133, 83, 95, 173)); Assert.AreEqual(173, rgbt.rgbtBlue); Assert.AreEqual(95, rgbt.rgbtGreen); Assert.AreEqual(83, rgbt.rgbtRed); rgbt.Color = Color.Crimson; Assert.That(EqualColors(Color.Crimson, rgbt.Color)); rgbt.Color = Color.MidnightBlue; Assert.That(EqualColors(Color.MidnightBlue, rgbt.Color)); rgbt.Color = Color.White; Assert.AreEqual(255, rgbt.rgbtBlue); Assert.AreEqual(255, rgbt.rgbtGreen); Assert.AreEqual(255, rgbt.rgbtRed); rgbt.Color = Color.Black; Assert.AreEqual(0, rgbt.rgbtBlue); Assert.AreEqual(0, rgbt.rgbtGreen); Assert.AreEqual(0, rgbt.rgbtRed); rgbt = Color.DarkGoldenrod; Color color = rgbt; Assert.That(EqualColors(Color.DarkGoldenrod, color)); }
private void SetColorChannels(int redmask, int greenmask, int bluemask) { if (Bitmap) { // Create a temporary clone. using (FreeImageBitmap bitmap = (FreeImageBitmap)this.bitmap.Clone()) { if (bitmap != null) { // Check whether the bitmap has a palette if (bitmap.HasPalette) { // Use the Palette class to handle the bitmap's // palette. A palette always consist of RGBQUADs. Palette palette = bitmap.Palette; // Apply the new values for all three color components. for (int i = 0; i < palette.Length; i++) { RGBQUAD rgbq = palette[i]; rgbq.rgbRed = (byte)(rgbq.rgbRed & redmask); rgbq.rgbGreen = (byte)(rgbq.rgbGreen & greenmask); rgbq.rgbBlue = (byte)(rgbq.rgbBlue & bluemask); palette[i] = rgbq; } } // In case the bitmap has no palette it must have a color depth // of 16, 24 or 32. Each color depth needs a different wrapping // structure for the bitmaps data. These structures can be accessed // by using the foreach clause. else if (bitmap.ColorDepth == 16) { // Iterate over each scanline // For 16bpp use either Scanline<FI16RGB555> or Scanline<FI16RGB565> if (bitmap.IsRGB555) { foreach (Scanline <FI16RGB555> scanline in bitmap) { for (int x = 0; x < scanline.Length; x++) { FI16RGB555 pixel = scanline[x]; pixel.Red = (byte)(pixel.Red & redmask); pixel.Green = (byte)(pixel.Green & greenmask); pixel.Blue = (byte)(pixel.Blue & bluemask); scanline[x] = pixel; } } } else if (bitmap.IsRGB565) { foreach (Scanline <FI16RGB565> scanline in bitmap) { for (int x = 0; x < scanline.Length; x++) { FI16RGB565 pixel = scanline[x]; pixel.Red = (byte)(pixel.Red & redmask); pixel.Green = (byte)(pixel.Green & greenmask); pixel.Blue = (byte)(pixel.Blue & bluemask); scanline[x] = pixel; } } } } else if (bitmap.ColorDepth == 24) { // Iterate over each scanline // For 24bpp Scanline<RGBTRIPLE> must be used foreach (Scanline <RGBTRIPLE> scanline in bitmap) { for (int x = 0; x < scanline.Length; x++) { RGBTRIPLE pixel = scanline[x]; pixel.rgbtRed = (byte)(pixel.rgbtRed & redmask); pixel.rgbtGreen = (byte)(pixel.rgbtGreen & greenmask); pixel.rgbtBlue = (byte)(pixel.rgbtBlue & bluemask); scanline[x] = pixel; } } } else if (bitmap.ColorDepth == 32) { // Iterate over each scanline // For 32bpp Scanline<RGBQUAD> must be used foreach (Scanline <RGBQUAD> scanline in bitmap) { for (int x = 0; x < scanline.Length; x++) { RGBQUAD pixel = scanline[x]; pixel.rgbRed = (byte)(pixel.rgbRed & redmask); pixel.rgbGreen = (byte)(pixel.rgbGreen & greenmask); pixel.rgbBlue = (byte)(pixel.rgbBlue & bluemask); scanline[x] = pixel; } } } // Dispose only the picturebox's bitmap if (pictureBox.Image != null) { pictureBox.Image.Dispose(); } pictureBox.Image = (Bitmap)bitmap; } } } }