Shalvin.Com                                                                                                                      Home
Code Snippets

Serializing an Object in .Net

//
Person.cs
using System.Collections.Generic;
using System.Text;
using System.Runtime.Serialization;

namespace SerializingClass
 {
[Serializable] class Person {
public string name;
public string Specialization;
public Person(string _name, string _Specialization)
{ name = _name;
Specialization = _Specialization;
}
public Person() { }
public override string ToString()
{ return name + " is Specializting in " + Specialization ; }
 }
}

//Form
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;

private void btnSerialize_Click(object sender, EventArgs e)
{
Person sp = new Person("Shalvin", "ASP.Net");
// Create file to save the data to
FileStream fs = new FileStream("c:\\Person.Dat", FileMode.Create);
// Create a BinaryFormatter object to perform the serialization
BinaryFormatter bf = new BinaryFormatter();
// Use the BinaryFormatter object to serialize the data to the file
bf.Serialize(fs, sp);
// Close the file
fs.Close();
MessageBox.Show("File serialized");
System.Diagnostics.Process.Start("notepad", "c:\\Person.Dat");
}

Gdi+ in Asp.Net

using System.Drawing.Imaging;
public partial class _1DrawString : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Bitmap objBitmap;
Graphics objGraphics;
objBitmap = new Bitmap(400, 200);
objGraphics = Graphics.FromImage(objBitmap);
objGraphics.DrawLine(Pens.Blue, 50, 50, 50, 200);
objBitmap.Save(Response.OutputStream, ImageFormat.Gif);
}
}

Gdi+ Cropping an Image (VB.Net)
Imports System.Drawing.Imaging
Public Class Form1
Private Sub btnCrop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCrop.Click
Dim myBitmap As Bitmap
Dim myBitmapCropped As Bitmap
Dim myGraphics As Graphics
Try
Dim theRectangle As Rectangle = New Rectangle(0, 0, 250, 300)
' Open the original image
myBitmap = New Bitmap("d:\ShalvinSmall.jpg") '
Create a second bitmap that serves as the canvas that the cropped image will be placed on
myBitmapCropped = New Bitmap(250, 300)
myGraphics = Graphics.FromImage(myBitmapCropped)
myGraphics.DrawImage(myBitmap, New Rectangle(0, 0, myBitmapCropped.Width,  myBitmapCropped.Height), theRectangle.Left, theRectangle.Top, theRectangle.Width, theRectangle.Height, GraphicsUnit.Pixel)
myBitmap.Dispose()
myBitmapCropped.Save("d:\Sh.jpg", ImageFormat.Jpeg)
System.Diagnostics.Process.Start("iexplore", "d:\Sh.jpg")
Catch ex As Exception MessageBox.Show(ex.Message)
End Try
End Sub
 shalvin@gmail.com