1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
|
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
using System.IO;
using System.Drawing.Text;
using System.Drawing.Drawing2D;
namespace WinEmfToPNG
{
public partial class Form1 : Form
{
private string pathFile = Directory.GetCurrentDirectory();
public Form1()
{
InitializeComponent();
}
// en portee class cause du playrecord
private Metafile wmf;
private void button1_Click(object sender, EventArgs e)
{
DisplayImage();
}
private Metafile wmf;
private void DisplayImage()
{
wmf = new Metafile(pathFile + @"\DessinTest.emf");
Graphics.EnumerateMetafileProc metafileDelegate =
new Graphics.EnumerateMetafileProc(MetafileCallback);
//ESPIONNE LA TAILLE MAXIMALE DU METAFILE POUR LE TILER
//EVENTUELLEMENT
this.Text = wmf.Height.ToString() + wmf.Width.ToString();
//RECT SOURCE
Rectangle srcRect = new Rectangle(500,500, 1500,1500);
//RECT DESTINATION : meme size
Rectangle destRect = new Rectangle(0, 0,srcRect.Width,srcRect.Height );
// BMP: meme size que destRect
Bitmap bmp = new Bitmap(destRect.Width, destRect.Height);
Graphics grBmp = Graphics.FromImage(bmp);
grBmp.CompositingQuality=CompositingQuality.HighQuality;
//DESSIN: encore une fois pas besoin de g.DrawImage !!!!..
grBmp.EnumerateMetafile(wmf, destRect, srcRect, GraphicsUnit.Pixel,
metafileDelegate);
grBmp.Dispose();
bmp.Save(pathFile + @"\part.png", ImageFormat.Png);
this.pictureBox1.Image = bmp;
}
private bool MetafileCallback(
EmfPlusRecordType recordType,
int flags,
int dataSize,
IntPtr data,
PlayRecordCallback callbackData)
{
byte[] dataArray = null;
if (data != IntPtr.Zero)
{
// Copy the unmanaged record to a managed byte buffer
// that can be used by PlayRecord.
dataArray = new byte[dataSize];
Marshal.Copy(data, dataArray, 0, dataSize);
}
// "play" records sur le graphics
wmf.PlayRecord(recordType, flags, dataSize, dataArray);
return true;
}
}
} |
Partager