Created
July 6, 2017 15:19
-
-
Save jolovin-mmi/19d90d5ed9a51e2be02ac53ba6e5f407 to your computer and use it in GitHub Desktop.
Draws a color map of the colors in a Windows MetaFile
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Drawing; | |
using System.Drawing.Imaging; | |
using System.IO; | |
using System.Runtime.InteropServices; | |
namespace WmfColorChecker | |
{ | |
public class Program | |
{ | |
private static Metafile _wmf; | |
private static List<Color> foundColors; | |
public static void Main(string[] args) | |
{ | |
var wmfFilePath = @"1708839-16.wmf"; | |
foundColors = new List<Color>(); | |
TestWmf(wmfFilePath); | |
DrawColorMap(wmfFilePath); | |
} | |
private static void TestWmf(string filePath) | |
{ | |
_wmf = new Metafile(filePath); | |
using (var bitmap = new Bitmap(_wmf.Width, _wmf.Height)) | |
{ | |
using (var g = Graphics.FromImage(bitmap)) | |
{ | |
var callBack = new Graphics.EnumerateMetafileProc(wmfCallback); | |
g.EnumerateMetafile(_wmf, new Rectangle(0, 0, _wmf.Width, _wmf.Height), callBack); | |
} | |
} | |
_wmf.Dispose(); | |
} | |
private static bool wmfCallback(EmfPlusRecordType recordType, int flags, int dataSize, IntPtr data, PlayRecordCallback callbackData) | |
{ | |
byte[] recordData = null; | |
if (data != IntPtr.Zero) | |
{ | |
recordData = new byte[dataSize]; | |
Marshal.Copy(data, recordData, 0, dataSize); | |
} | |
if (recordType == EmfPlusRecordType.WmfCreateBrushIndirect) | |
{ | |
foundColors.Add(Color.FromArgb(recordData[2], recordData[3], recordData[4])); | |
} | |
else if (recordType == EmfPlusRecordType.WmfCreateBrushIndirect) | |
{ | |
foundColors.Add(Color.FromArgb(recordData[6], recordData[7], recordData[8])); //gets outline colors, if any | |
} | |
_wmf.PlayRecord(recordType, flags, dataSize, recordData); | |
return true; | |
} | |
private static void DrawColorMap(string filePath) | |
{ | |
using (var bitmap = new Bitmap(100, 100 * foundColors.Count + 1)) | |
{ | |
using (var g = Graphics.FromImage(bitmap)) | |
{ | |
var x = 0; | |
var y = 0; | |
foreach (var foundColor in foundColors) | |
{ | |
g.FillRectangle(new SolidBrush(foundColor), x, y, 100, 100); | |
y += 100; | |
} | |
} | |
var newFile = string.Format("{0}-colormap.png", Path.GetFileNameWithoutExtension(filePath)); | |
if (File.Exists(newFile)) | |
{ | |
File.Delete(newFile); | |
} | |
bitmap.Save(newFile, ImageFormat.Png); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment