Bad Broadcast

   


using System;
using System.Net;
using System.Net.Sockets;
using System.Text;

public class BadBroadcast {
   public static void Main()
   {
      Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
      IPEndPoint iep = new IPEndPoint(IPAddress.Broadcast, 9050);

      byte[] data = Encoding.ASCII.GetBytes("This is a test message");
      sock.SendTo(data, iep);
      sock.Close();
   }
}

           
          


Receive Broadcast

   

/*
C# Network Programming 
by Richard Blum

Publisher: Sybex 
ISBN: 0782141765
*/
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;

public class RecvBroadcst
{
   public static void Main()
   {
      Socket sock = new Socket(AddressFamily.InterNetwork,
                      SocketType.Dgram, ProtocolType.Udp);
      IPEndPoint iep = new IPEndPoint(IPAddress.Any, 9050);
      sock.Bind(iep);
      EndPoint ep = (EndPoint)iep;
      Console.WriteLine("Ready to receive...");

      byte[] data = new byte[1024];
      int recv = sock.ReceiveFrom(data, ref ep);
      string stringData = Encoding.ASCII.GetString(data, 0, recv);
      Console.WriteLine("received: {0}  from: {1}",
                            stringData, ep.ToString());

      data = new byte[1024];
      recv = sock.ReceiveFrom(data, ref ep);
      stringData = Encoding.ASCII.GetString(data, 0, recv);
      Console.WriteLine("received: {0}  from: {1}",
                            stringData, ep.ToString());
      sock.Close();
   }
}

           
          


Create Metafile

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); } } [/csharp]

Create Metafile (Reload)

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); } } [/csharp]

Create Metafile (Memory)

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); } } [/csharp]

Metafile Page Units

   
 
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);
     }      
}

    


Enumerate Metafile

   
 

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("&amp;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);
     }
}