如何灰度显示emf图像

时间:2013-07-20 18:30:07

标签: grayscale metafile

使用C#和Visual Studio 2010,如何从彩色电影制作灰度级电动车?我应该以某种方式枚举记录并更改颜色设置吗?

结果我想要一个新的电影图像。

1 个答案:

答案 0 :(得分:0)

这是一个开源的emf读/写实用程序。我不认为可以使用GDI +来保存矢量内容。所以这有望帮助你,但我没有远程测试所有可能的电动车案例。

https://wmf.codeplex.com/

这适用于wmf和emf格式。我们可以将Oxage.Wmf策略与来自其他帖子的策略结合起来,该策略在创建时基本上重新着色笔或画笔,以便用该笔或画笔绘制的任何内容都应该是灰色的。

using Oxage.Wmf;
using Oxage.Wmf.Records;
using Oxage.Wmf.Objects;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MetafileTest
{
    public class GrayMap
    {
        public void GrayFile(string sourceFile, string destFile){

            WmfDocument wmf = new WmfDocument();
            wmf.Load(sourceFile);

            foreach (var record in wmf.Records)
            {
                if (record is WmfCreateBrushIndirectRecord)
                {
                    var brush = record as WmfCreateBrushIndirectRecord;
                    brush.Color = Gray(brush.Color);
                }
                else if (record is WmfCreatePenIndirectRecord)
                {
                    var pen = record as WmfCreatePenIndirectRecord;
                    pen.Color = Gray(pen.Color);
                }
                else if (record is WmfCreatePalette) {
                    var pal = record as WmfCreatePalette;
                    foreach (PaletteEntry entry in pal.Palette.PaletteEntries) {
                        Color test = Color.FromArgb(entry.Red, entry.Green, entry.Blue);
                        Color grayTest = Gray(test);
                        entry.Red = grayTest.R;
                        entry.Green = grayTest.G;
                        entry.Blue = grayTest.B;
                    }
                }
            }
            wmf.Save(destFile);

        }

        public Color Gray(Color original) { 
            int r = (int)(original.R*0.2989);
            int g = (int)(original.G*0.5870);
            int b = (int)(original.B*0.1140);
            Color result = Color.FromArgb(r, g, b);
            return result;

        }

    }
}

这是通过以下填充蓝色矩形的超简单案例进行测试的,因此至少在简单的情况下这将起作用。它可能无法处理所有情况,但在这种情况下,您可以扩展原始源以满足您的需求,因为它是开源的。

    private void button1_Click(object sender, EventArgs e)
    {
        var wmf = new WmfDocument();
        wmf.Width = 1000;
        wmf.Height = 1000;
        wmf.Format.Unit = 288;
        wmf.AddPolyFillMode(PolyFillMode.WINDING);
        wmf.AddCreateBrushIndirect(Color.Blue, BrushStyle.BS_SOLID);
        wmf.AddSelectObject(0);
        wmf.AddCreatePenIndirect(Color.Black, PenStyle.PS_SOLID, 1);
        wmf.AddSelectObject(1);
        wmf.AddRectangle(100, 100, 800, 800);
        wmf.AddDeleteObject(0);
        wmf.AddDeleteObject(1);
        wmf.Save("D:\\test.emf");
    }

    private void button2_Click(object sender, EventArgs e)
    {
        GrayMap map = new GrayMap();
        map.GrayFile("D:\\test.emf", "D:\\test2.emf");
    }