/*
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();
}
}
Software Development
software development notes, main category for Cs, Java, and other programming experiences.
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("&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);
}
}
Rotate Transform
/*
User Interfaces in C#: Windows Forms and Custom Controls
by Matthew MacDonald
Publisher: Apress
ISBN: 1590590457
*/
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Drawing.Text;
namespace GDI_Basics
{
/// <summary>
/// Summary description for RotateTransform.
/// </summary>
public class RotateTransform : System.Windows.Forms.Form
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public RotateTransform()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
//
// RotateTransform
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Name = "RotateTransform";
this.Text = "RotateTransform";
this.Paint += new System.Windows.Forms.PaintEventHandler(this.RotateTransform_Paint);
}
#endregion
[STAThread]
static void Main()
{
Application.Run(new RotateTransform());
}
private void RotateTransform_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
// Optimize text quality.
e.Graphics.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;
// Move origin to center of form so we can rotate around that.
e.Graphics.TranslateTransform(this.Width / 2 - 30, this.Height / 2 - 30);
DrawText(e.Graphics);
e.Graphics.RotateTransform(45);
DrawText(e.Graphics);
e.Graphics.RotateTransform(75);
DrawText(e.Graphics);
e.Graphics.RotateTransform(160);
DrawText(e.Graphics);
}
private void DrawText(Graphics g)
{
g.DrawString("Text", new Font("Verdana", 30, FontStyle.Bold),
Brushes.Black, 0, 10);
}
}
}