C#图像在保存时丢失颜色

时间:2016-01-14 17:28:57

标签: c# image printing colors save

我正在使用一个将图像发送到卡片打印机(Datacard品牌)的小应用程序,该程序应该将图像转换为仅两种颜色(就像黑白)RGB(217,217,217)和纯白色。问题是即使在我的屏幕中图片看起来已经转换,当我保存/打印图像时,它看起来是黑白的。

此方法将图像转换为黑白图像,以后我可以仅使用RGB 217,217,217

替换黑色
public static Bitmap BitmapTo1Bpp(Bitmap img)
{
        int w = img.Width;
        int h = img.Height;
        Bitmap bmp = new Bitmap(w, h, PixelFormat.Format1bppIndexed);
        BitmapData data = bmp.LockBits(new Rectangle(0, 0, w, h),ImageLockMode.ReadWrite, PixelFormat.Format1bppIndexed);
        byte[] scan = new byte[(w + 7) / 8];
        for (int y = 0; y < h; y++)
        {
            for (int x = 0; x < w; x++)
            {
                if (x % 8 == 0) scan[x / 8] = 0;
                Color c = img.GetPixel(x, y);
                if (c.GetBrightness() >= 0.5) scan[x / 8] |= (byte)(0x80 >> (x % 8));
            }
            Marshal.Copy(scan, 0, (IntPtr)((long)data.Scan0 + data.Stride * y), scan.Length);
        }
        bmp.UnlockBits(data);
        return bmp;
}

此方法替换新颜色的黑色:

public static Image ImageToRGB217_2(Image image)
    {
        ColorMatrix colorMatrix = new ColorMatrix(new[]
        {
            new float[] {0.851f, 0.851f, 0.851f, 1.000f, 0.000f},
            new float[] {0.851f, 0.851f, 0.851f, 1.000f, 0.000f},
            new float[] {0.851f, 0.851f, 0.851f, 1.000f, 0.000f},
            new float[] {0.000f, 0.000f, 0.000f, 0.000f, 0.000f},
            new float[] {1.000f, 1.000f, 1.000f, 0.000f, 0.000f}
        });

        Image img = ApplyColorMatrix(image, colorMatrix);
        return img;
    }

private static Bitmap ApplyColorMatrix(Image sourceImage, ColorMatrix colorMatrix)
    {
        Bitmap bmp32BppSource = GetArgbCopy(sourceImage);
        Bitmap bmp32BppDest = new Bitmap(bmp32BppSource.Width, bmp32BppSource.Height, PixelFormat.Format32bppArgb);

        try
        {
            using (Graphics graphics = Graphics.FromImage(bmp32BppDest))
            {
                ImageAttributes bmpAttributes = new ImageAttributes();
                bmpAttributes.SetColorMatrix(colorMatrix);

                graphics.DrawImage(bmp32BppSource, new Rectangle(0, 0, bmp32BppSource.Width, bmp32BppSource.Height),
                    0, 0, bmp32BppSource.Width, bmp32BppSource.Height, GraphicsUnit.Pixel, bmpAttributes);

            }

            bmp32BppSource.Dispose();
        }
        catch (Exception ex)
        {
            MessageBox.Show(@"Ocurrió un error al intentar aplicar el filtro UV en una de las imágenes, error:" + ex.Message, "Convertidor", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        return bmp32BppDest;
    }

private static Bitmap GetArgbCopy(Image sourceImage)
    {
        Bitmap bmpNew = new Bitmap(sourceImage.Width, sourceImage.Height, PixelFormat.Format32bppArgb);

        using (Graphics graphics = Graphics.FromImage(bmpNew))
        {
            graphics.DrawImage(sourceImage, new Rectangle(0, 0, bmpNew.Width, bmpNew.Height), new Rectangle(0, 0, bmpNew.Width, bmpNew.Height), GraphicsUnit.Pixel);
            graphics.Flush();
        }

        return bmpNew;
    }

这是我打电话的地方:

private void usarFiltroRGB217ToolStripMenuItem_Click(object sender, EventArgs e)
    {
        pictureBox1.Image = UVImage.ImageToRGB217_2(UVImage.BitmapTo1Bpp((Bitmap)pictureBox1.Image));
        pictureBox1.Refresh();
    }

这是结果在屏幕上的显示方式:http://i127.photobucket.com/albums/p128/dawc159951/Preview_zpsxqtzht2a.jpg

但是当我保存/打印时,这就是我得到的: http://i127.photobucket.com/albums/p128/dawc159951/217_zpsbmuobnrw.png

这是保存方法:

if (saveFileDialog1.ShowDialog() == DialogResult.OK)
        {
            string path = saveFileDialog1.FileName;
            Bitmap tmp = (Bitmap)pictureBox1.Image;
            tmp.Save(path, ImageFormat.Bmp);
            if (File.Exists(path))
                Process.Start(path);
        }

我很感激任何帮助,以找出为什么我无法得到我需要的结果。

提前致谢。

0 个答案:

没有答案