使用C#删除图像中的透明度

时间:2009-03-06 09:51:59

标签: c# .net image transparency

有没有人知道一种平滑/快速的方式来消除透明度,例如pngs / tiffs等用白色背景替换它?

基本上我需要的是我需要创建PDF / A兼容图像,根据规范,它可能具有-no-透明度(因此固定的白色背景很好)。

有任何想法/建议吗?

干杯&谢谢, -Jörg

6 个答案:

答案 0 :(得分:15)

你可以创建一个与png大小相同的位图,绘制一个白色矩形,然后在它上面绘制图像。

void RemTransp(string file) {
    Bitmap src = new Bitmap(file);
    Bitmap target = new Bitmap(src.Size.Width,src.Size.Height);
    Graphics g = Graphics.FromImage(target);
    g.DrawRectangle(new Pen(new SolidBrush(Color.White)), 0, 0, target.Width, target.Height);
    g.DrawImage(src, 0, 0);
    target.Save("Your target path");
}

答案 1 :(得分:5)

您必须删除Alpha通道。否则你仍然会有一个透明的图像 - 只是没有透明区域。

fs.truncate("persistence\\announce.txt", 0, function() {
    fs.writeFile("persistence\\announce.txt", string, function (err) {
        if (err) {
            return console.log("Error writing file: " + err);
        }
    });
});

答案 2 :(得分:0)

1)创建一个白色背景且尺寸与图像相同的位图 2)加载图像并将其绘制在“白色”位图的上方 3)保存新创建的图像

答案 3 :(得分:0)

PNG具有alpha通道,因此简单的重新着色不会。创建相同大小的白色图像,并创建合成图像覆盖图像。

答案 4 :(得分:0)

添加到Stormenet的答案;记得用“using”语句包装所有Bitmap和Graphics对象以处理非托管资源。

答案 5 :(得分:0)

名称空间:

using Microsoft.Win32;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using PdfSharp;
using PdfSharp.Pdf;
using PdfSharp.Drawing;

从File:

创建PNG或TIFF BitmapSource
BitmapSource BitmapSource;
private void OpenFile(Object sender, RoutedEventArgs e)
    {
        OpenFileDialog OpenFileDialog = new OpenFileDialog();
        OpenFileDialog.Filter = "PNG files (*.png)|*.png|TIFF files (*.tif)|*.tif";            

        if (OpenFileDialog.ShowDialog() == true)
        {
            try
            {
                if (OpenFileDialog.OpenFile() != null)
                {
                    String InitialPath = OpenFileDialog.FileName;                       
                    FileStream InitialFileStream = new FileStream(InitialPath, FileMode.Open, FileAccess.Read, FileShare.Read);

                    switch (OpenFileDialog.FilterIndex)
                    {
                        case 1:
                            PngBitmapDecoder PngBitmapDecoder = new PngBitmapDecoder(InitialFileStream, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad);
                            BitmapSource = PngBitmapDecoder.Frames[0];
                            InitialFileStream.Close();
                            break;
                        case 2:
                            TiffBitmapDecoder TiffBitmapDecoder = new TiffBitmapDecoder(InitialFileStream, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad);
                            BitmapSource = TiffBitmapDecoder.Frames[0];
                            InitialFileStream.Close();
                            break;
                    }  
                }
            }
            catch (Exception Exception)
            {
                MessageBox.Show("Error: Could not read file from disk. Original error: ", Exception.Message, MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
    }

按钮上的功能点击:

private void ButtonClick(Object sender, RoutedEventArgs e)
    {
        PixelFormat PixelFormat = BitmapSource.Format;
        if (PixelFormat == PixelFormats.Bgra32)
        {
            try
            {
                BitmapSource = Bgra32ToBgra24(BitmapSource);
                //BitmapSource = Bgra32ToGray8(BitmapSource);
            }

            catch (Exception Exception)
            {
                MessageBox.Show("Error: Could not convert BitmapSource. Original error: ", Exception.Message, MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
    }

功能:

public static BitmapSource Bgra32ToBgr24(BitmapSource BitmapSource)
    {
        Int32 PixelWidth = BitmapSource.PixelWidth;
        Int32 PixelHeight = BitmapSource.PixelHeight;
        Double DpiX = BitmapSource.DpiX;
        Double DpiY = BitmapSource.DpiY;

        PixelFormat InputPixelFormat = BitmapSource.Format;
        BitmapPalette InputPalette = BitmapSource.Palette;
        Int32 InputBitsPerPixel = BitmapSource.Format.BitsPerPixel;
        Int32 InputStride = PixelWidth * InputBitsPerPixel / 8;
        Byte[] InputPixelsArray = new Byte[InputStride * PixelHeight];
        BitmapSource.CopyPixels(InputPixelsArray, InputStride, 0);

        PixelFormat PixelFormat = PixelFormats.Bgr24;
        BitmapPalette Palette = null;
        Int32 BitsPerPixel = 24;
        Int32 Stride = PixelWidth * BitsPerPixel / 8;
        Byte[] PixelsArray = new Byte[InputStride * PixelHeight / 4 * 3]; 

        Int32 i = 0; Int32 j = 0; Int32 k = 0;
        while (i < InputPixelsArray.Length / 4)
        {
            PixelsArray[k] = InputPixelsArray[j];
            PixelsArray[k + 1] = InputPixelsArray[j + 1];
            PixelsArray[k + 2] = InputPixelsArray[j + 2];

            i = i + 1;
            j = j + 4;
            k = k + 3;
        }

        BitmapSource = BitmapSource.Create(PixelWidth, PixelHeight, DpiX, DpiY, PixelFormat, Palette, PixelsArray, Stride);
        return BitmapSource;
    }

将频道转换为Gray8 BitmapSource

public static BitmapSource Bgra32ToGray8(BitmapSource BitmapSource)
    {
        Int32 PixelWidth = BitmapSource.PixelWidth;
        Int32 PixelHeight = BitmapSource.PixelHeight;
        Double DpiX = BitmapSource.DpiX;
        Double DpiY = BitmapSource.DpiY;

        PixelFormat InputPixelFormat = BitmapSource.Format;
        BitmapPalette InputPalette = BitmapSource.Palette;
        Int32 InputBitsPerPixel = BitmapSource.Format.BitsPerPixel;
        Int32 InputStride = PixelWidth * InputBitsPerPixel / 8;
        Byte[] InputPixelsArray = new Byte[InputStride * PixelHeight];
        BitmapSource.CopyPixels(InputPixelsArray, InputStride, 0);

        PixelFormat PixelFormat = PixelFormats.Gray8;
        BitmapPalette Palette = null;
        Int32 BitsPerPixel = 8;
        Int32 Stride = PixelWidth * BitsPerPixel / 8;
        Byte[] A_PixelsArray = new Byte[InputStride * PixelHeight / 4];

        Int32 i = 0; Int32 j = 3;
        while (i < InputPixelsArray.Length / 4)
        {
            A_PixelsArray[i] = InputPixelsArray[j];

            i = i + 1;
            j = j + 4;
        }

        BitmapSource = BitmapSource.Create(PixelWidth, PixelHeight, DpiX, DpiY, PixelFormat, Palette, A_PixelsArray, Stride);
        return BitmapSource;
    }

将BitmapSource保存为PDF:

private void SaveFileAs(Object sender, RoutedEventArgs e)
    {
        SaveFileDialog SaveFileDialog = new SaveFileDialog();
        SaveFileDialog.Filter = "PDF files (*.pdf)|*.pdf";
        if (SaveFileDialog.ShowDialog() == true)
        {
            try
            {
                if (SaveFileDialog.FileName != null)
                {
                    String DestinationPath = SaveFileDialog.FileName;
                    FileStream DestinationFileStream = new FileStream(DestinationPath, FileMode.Create, FileAccess.Write, FileShare.Write);

                    switch (SaveFileDialog.FilterIndex)
                    {   
                        case 1:
                            PdfDocument PdfDocument = new PdfDocument();
                            PdfPage PdfPage = new PdfPage();
                            PdfDocument.Pages.Add(PdfPage);
                            XImage Image = XImage.FromBitmapSource(BitmapSource);
                            XGraphics XGraphic = XGraphics.FromPdfPage(PdfDocument.Pages[0]);

                            Double VerticalMargin = 20;
                            Double HorizontalMargin = 20;
                            Double Ratio = BitmapSource.Height / BitmapSource.Width;
                            Double ImageWidth = PdfPage.Width - 2 * VerticalMargin;
                            Double ImageHeight = Ratio * (PdfPage.Width - 2 * HorizontalMargin);

                            XGraphic.DrawImage(Image, VerticalMargin, HorizontalMargin, ImageWidth, ImageHeight);
                            PdfDocument.Save(DestinationFileStream);
                            PdfDocument.Close();
                            DestinationFileStream.Close();
                            break;
                    }
                }
            }
            catch (Exception Exception)
            {
                MessageBox.Show("Error: Could not write file to disk. Original error: ", Exception.Message, MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
    }