Hi,
I am trying to change the color of an existing metafile(.emf) file from my machine and save it as another modified copy. I am able to render the run time metafile on the windows form but haven't been able to save it on my machine.
I have attached my code below and would be grateful if somebody can help.
What I have tried:
[DllImport("gdi32.dll")]
static extern IntPtr CopyEnhMetaFile(
IntPtr hemfSrc,
String lpszFile
);
[DllImport("gdi32.dll")]
static extern int DeleteEnhMetaFile(
IntPtr hemf
);
[DllImport("gdi32.dll")]
static extern uint GetEnhMetaFileBits(IntPtr hemf, uint cbBuffer,
[Out] byte[] lpbBuffer);
private void PicModified_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
float[][] matrixColTrans =
{
new float[] {0, 0, 0, 0, 0}
, new float[] {0, 0, 0, 0, 0}
, new float[] {0, 0, 0, 0, 0}
, new float[] {0, 204, 0, 1, 0}
, new float[] {0, 0, 0, 0, 0}
};
ColorMatrix colorMatrix = new ColorMatrix(matrixColTrans);
ImageAttributes ia = new ImageAttributes();
ia.SetColorMatrix(colorMatrix);
if (txtSelectEMFFilePath.Text.EndsWith(".emf"))
{
string destFileName = txtSelectEMFFilePath.Text;
Metafile mf = new Metafile(destFileName);
g.DrawImage(
mf
, new Rectangle(0, 0, picModified.Width, picModified.Height)
, 0, 0
, mf.PhysicalDimension.Width
, mf.PhysicalDimension.Height
, GraphicsUnit.Pixel
, ia
);
IntPtr iptrMetafileHandle = mf.GetHenhmetafile();
IntPtr iptrMetafileHandleCopy = CopyEnhMetaFile(iptrMetafileHandle, "image.emf");
Image newimg = new Metafile("image.emf");
imgOut.Image = newimg;
imgOut.SizeMode = PictureBoxSizeMode.Zoom;
DeleteEnhMetaFile(iptrMetafileHandle);
DeleteEnhMetaFile(iptrMetafileHandleCopy);
mf.Dispose();
}
}