Csharp/C Sharp/2D Graphics/wmf file

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Create Metafile

<source lang="csharp"> using System; using System.Drawing; using System.Drawing.Imaging; using System.IO; using System.Windows.Forms;

class CreateMetafile: Form {

    Metafile mf;
  
    public static void Main()
    {
         Application.Run(new CreateMetafile());
    }
    public CreateMetafile()
    {
         ResizeRedraw = true;
         Graphics grfx  = CreateGraphics();
         IntPtr ipHdc = grfx.GetHdc();
  
         mf = new Metafile("CreateMetafile.emf", ipHdc);
  
         grfx.ReleaseHdc(ipHdc);
         grfx.Dispose();
  
         grfx = Graphics.FromImage(mf);
  
         grfx.FillEllipse(Brushes.Blue, 60, 20, 20, 20);
         grfx.Dispose();
    }
    protected override void OnPaint(PaintEventArgs pea)
    {
         DoPage(pea.Graphics, ForeColor,ClientSize.Width, ClientSize.Height);
    }  
    protected void DoPage(Graphics grfx, Color clr, int cx, int cy)
    {
         for (int y = 0; y < cy; y += mf.Height)
         for (int x = 0; x < cx; x += mf.Width)
              grfx.DrawImage(mf, x, y, mf.Width, mf.Height);
    }

}

</source>


Create Metafile (Memory)

<source lang="csharp"> using System; using System.Drawing; using System.Drawing.Imaging; using System.IO; using System.Windows.Forms;

class CreateMetafileMemory: Form {

    MemoryStream ms = new MemoryStream();
  
    public static void Main()
    {
         Application.Run(new CreateMetafileMemory());
    }
    public CreateMetafileMemory()
    {
         ResizeRedraw = true; 
  
         Graphics grfx = CreateGraphics();
         IntPtr ipHdc = grfx.GetHdc();
  
         Metafile mf = new Metafile(ms, ipHdc); 
  
         grfx.ReleaseHdc(ipHdc);
         grfx.Dispose();
         grfx = Graphics.FromImage(mf);
  
         grfx.FillEllipse(Brushes.Gray, 0, 0, 100, 100);
         grfx.DrawEllipse(Pens.Black, 0, 0, 100, 100);
         grfx.DrawArc(new Pen(Color.Red, 10), 20, 20, 60, 60, 30, 120);
         grfx.Dispose();
    }
    protected override void OnPaint(PaintEventArgs pea)
    {
         DoPage(pea.Graphics, ForeColor,ClientSize.Width, ClientSize.Height);
    }        
    protected void DoPage(Graphics grfx, Color clr, int cx, int cy)
    {
         ms.Position = 0;
         Metafile mf = new Metafile(ms);
  
         for (int y = 0; y < cy; y += mf.Height)
         for (int x = 0; x < cx; x += mf.Width)
              grfx.DrawImage(mf, x, y, mf.Width, mf.Height);
    }

}

</source>


Create Metafile (Reload)

<source lang="csharp"> using System; using System.Drawing; using System.Drawing.Imaging; using System.IO; using System.Windows.Forms; class CreateMetafileReload : Form {

   const string strMetafile = "CreateMetafileReload.emf";
   public static void Main() {
       Application.Run(new CreateMetafileReload());
   }
   public CreateMetafileReload() {
       ResizeRedraw = true;
       if (!File.Exists(strMetafile)) {
           Graphics grfx = CreateGraphics();
           IntPtr ipHdc = grfx.GetHdc();
           Metafile mf = new Metafile(strMetafile, ipHdc);
           grfx.ReleaseHdc(ipHdc);
           grfx.Dispose();
           grfx = Graphics.FromImage(mf);
           grfx.DrawEllipse(Pens.Black, 0, 0, 100, 100);
           grfx.FillEllipse(Brushes.Blue, 60, 20, 20, 20);
           grfx.DrawArc(new Pen(Color.Red, 10), 20, 20, 60, 60, 30, 120);
           grfx.Dispose();
       }
   }
   protected override void OnPaint(PaintEventArgs pea) {
       DoPage(pea.Graphics, ForeColor, ClientSize.Width, ClientSize.Height);
   }
   protected void DoPage(Graphics grfx, Color clr, int cx, int cy) {
       Metafile mf = new Metafile(strMetafile);
       for (int y = 0; y < cy; y += mf.Height)
           for (int x = 0; x < cx; x += mf.Width)
               grfx.DrawImage(mf, x, y, mf.Width, mf.Height);
   }

}

</source>


Enumerate Metafile

<source lang="csharp"> using System; using System.Drawing; using System.Drawing.Imaging; using System.IO; using System.Runtime.InteropServices; using System.Windows.Forms;

class EnumMetafile: Form {

    Metafile     mf;
    Panel        panel = new Panel();
  
    public static void Main()
    {
         Application.Run(new EnumMetafile());
    }
    public EnumMetafile()
    {
         Splitter splitter = new Splitter();
         splitter.Parent = this;
         splitter.Dock = DockStyle.Left; // Right;
  
         panel.Parent = this;
         panel.Dock = DockStyle.Left;
         panel.Paint += new PaintEventHandler(PanelOnPaint);
  
         Menu = new MainMenu();
         Menu.MenuItems.Add("&Open!", new EventHandler(MenuOpenOnClick));
    }
    void MenuOpenOnClick(object obj, EventArgs ea)
    {
         OpenFileDialog dlg = new OpenFileDialog();
         dlg.Filter = "All Metafiles|*.wmf;*.emf|" +
                      "Windows Metafile (*.wmf)|*.wmf|" +
                      "Enhanced Metafile (*.emf)|*.emf";
  
         if (dlg.ShowDialog() == DialogResult.OK)
         {
              mf = new Metafile(dlg.FileName);
              panel.Invalidate();
  
              Graphics grfx = CreateGraphics();
  
              grfx.EnumerateMetafile(mf, new Point(0, 0), 
                   new Graphics.EnumerateMetafileProc(EnumMetafileProc));
  
              grfx.Dispose();
         }
    }
    bool EnumMetafileProc(EmfPlusRecordType eprt, int iFlags,
                          int iDataSize, IntPtr ipData, 
                          PlayRecordCallback prc)
    {
         if (iDataSize > 0)
         {
              byte[] abyData = new Byte[iDataSize];
              Marshal.Copy(ipData, abyData, 0, iDataSize);
  
              foreach (byte by in abyData)
                   Console.WriteLine(" {0:X2}", by);
         }
         return true;
    }
    void PanelOnPaint(object obj, PaintEventArgs pea)
    {
         Panel    panel = (Panel) obj;
         Graphics grfx  = pea.Graphics;
  
         if (mf != null)
              grfx.DrawImage(mf, 0, 0);
    }

}

</source>


Metafile Page Units

<source lang="csharp"> using System; using System.Drawing; using System.Drawing.Imaging; using System.Drawing.Printing; using System.Windows.Forms;

class MetafilePageUnits: Form {

    Metafile mf;
  
    public static void Main()
    {
         Application.Run(new MetafilePageUnits());
    }
    public MetafilePageUnits()
    {
         ResizeRedraw = true; 
         Graphics grfx  = CreateGraphics();
         IntPtr ipHdc = grfx.GetHdc();
  
         mf = new Metafile("MetafilePageUnits.emf", ipHdc);
  
         grfx.ReleaseHdc(ipHdc);
         grfx.Dispose();
         grfx = Graphics.FromImage(mf);
         grfx.Clear(Color.White);
  
         grfx.PageUnit = GraphicsUnit.Pixel;
         Pen pen = new Pen(Color.Black, grfx.DpiX / 72);
         grfx.DrawRectangle(pen, 0, 0, grfx.DpiX, grfx.DpiY);
  
         grfx.PageUnit = GraphicsUnit.Inch;
         grfx.PageScale = 0.01f;
         pen = new Pen(Color.Black, 100f / 72);
         grfx.DrawRectangle(pen, 25, 25, 100, 100);
  
         grfx.Dispose();
    }
    protected void OnPaint(PaintEventArgs pea)
    {
         grfx.DrawImage(mf, 0, 0);
    }      

}

</source>