Saturday, May 30, 2009

Drop Shadow around Form

// Drop Shadow around Form

public partial class MyCustomForm : Form
{
private static Int32 CS_DROPSHADOW = 0x00020000;

protected override CreateParams CreateParams
{
get
{
CreateParams parameters = base.CreateParams;
parameters.ClassStyle |= CS_DROPSHADOW;

return parameters;
}
}
}

Captcha Image

// Captcha Image

public class CaptchaImage
{
// Public properties (all read-only).
public string Text
{
get { return this.text; }
}
public Bitmap Image
{
get { return this.image; }
}
public int Width
{
get { return this.width; }
}
public int Height
{
get { return this.height; }
}

// Internal properties.
private string text;
private int width;
private int height;
private string familyName;
private Bitmap image;

// For generating random numbers.
private Random random = new Random();

// ====================================================================
// Initializes a new instance of the CaptchaImage class using the
// specified text, width and height.
// ====================================================================
public CaptchaImage(int length, int width, int height) : this(length, width, height, null)
{
}

// ====================================================================
// Initializes a new instance of the CaptchaImage class using the
// specified text, width, height and font family.
// ====================================================================
public CaptchaImage(int length, int width, int height, string familyName)
{
this.text = GenerateRandomText(length);
this.SetDimensions(width, height);
this.SetFamilyName(familyName);
this.GenerateImage();
}

private string GenerateRandomText(int length)
{
Random rnd = new Random();
List table = new List();

for (char i = 'A'; i <= 'Z'; i++)
table.Add(i);

for (char i = '0'; i <= '9'; i++)
table.Add(i);

string retVal = "";

for (int i = 0; i < length; i++)
retVal += table[rnd.Next(table.Count)];

return retVal;
}

// ====================================================================
// This member overrides Object.Finalize.
// ====================================================================
~CaptchaImage()
{
Dispose(false);
}

// ====================================================================
// Releases all resources used by this object.
// ====================================================================
public void Dispose()
{
GC.SuppressFinalize(this);
this.Dispose(true);
}

// ====================================================================
// Custom Dispose method to clean up unmanaged resources.
// ====================================================================
protected virtual void Dispose(bool disposing)
{
if (disposing)
// Dispose of the bitmap.
this.image.Dispose();
}

// ====================================================================
// Sets the image width and height.
// ====================================================================
private void SetDimensions(int width, int height)
{
// Check the width and height.
if (width <= 0)
throw new ArgumentOutOfRangeException("width", width, "Argument out of range, must be greater than zero.");
if (height <= 0)
throw new ArgumentOutOfRangeException("height", height, "Argument out of range, must be greater than zero.");
this.width = width;
this.height = height;
}

// ====================================================================
// Sets the font used for the image text.
// ====================================================================
private void SetFamilyName(string familyName)
{
// If the named font is not installed, default to a system font.
try
{
Font font = new Font(this.familyName, 12F);
this.familyName = familyName;
font.Dispose();
}
catch (Exception ex)
{
this.familyName = System.Drawing.FontFamily.GenericSerif.Name;
}
}

// ====================================================================
// Creates the bitmap image.
// ====================================================================
private void GenerateImage()
{
// Create a new 32-bit bitmap image.
Bitmap bitmap = new Bitmap(this.width, this.height, PixelFormat.Format32bppArgb);

// Create a graphics object for drawing.
Graphics g = Graphics.FromImage(bitmap);
g.SmoothingMode = SmoothingMode.AntiAlias;
Rectangle rect = new Rectangle(0, 0, this.width, this.height);

// Fill in the background.
HatchBrush hatchBrush = new HatchBrush(HatchStyle.SmallConfetti, Color.LightGray, Color.White);
g.FillRectangle(hatchBrush, rect);

// Set up the text font.
SizeF size;
float fontSize = rect.Height + 1;
Font font;
// Adjust the font size until the text fits within the image.
do
{
fontSize--;
font = new Font(this.familyName, fontSize, FontStyle.Bold);
size = g.MeasureString(this.text, font);
} while (size.Width > rect.Width);

// Set up the text format.
StringFormat format = new StringFormat();
format.Alignment = StringAlignment.Center;
format.LineAlignment = StringAlignment.Center;

// Create a path using the text and warp it randomly.
GraphicsPath path = new GraphicsPath();
path.AddString(this.text, font.FontFamily, (int)font.Style, font.Size, rect, format);
float v = 4F;
PointF[] points =
{
new PointF(this.random.Next(rect.Width) / v, this.random.Next(rect.Height) / v),
new PointF(rect.Width - this.random.Next(rect.Width) / v, this.random.Next(rect.Height) / v),
new PointF(this.random.Next(rect.Width) / v, rect.Height - this.random.Next(rect.Height) / v),
new PointF(rect.Width - this.random.Next(rect.Width) / v, rect.Height - this.random.Next(rect.Height) / v)
};
Matrix matrix = new Matrix();
matrix.Translate(0F, 0F);
path.Warp(points, rect, matrix, WarpMode.Perspective, 0F);

// Draw the text.
hatchBrush = new HatchBrush(HatchStyle.LargeConfetti, Color.LightGray, Color.DarkGray);
g.FillPath(hatchBrush, path);

// Add some random noise.
int m = Math.Max(rect.Width, rect.Height);
for (int i = 0; i < (int)(rect.Width * rect.Height / 30F); i++)
{
int x = this.random.Next(rect.Width);
int y = this.random.Next(rect.Height);
int w = this.random.Next(m / 50);
int h = this.random.Next(m / 50);
g.FillEllipse(hatchBrush, x, y, w, h);
}

// Clean up.
font.Dispose();
hatchBrush.Dispose();
g.Dispose();

// Set the image.
this.image = bitmap;
}
}

Drawing a Gradient Background for a Control

using System;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;

namespace DevDistrict.Sample
{
public class GradientPanel : System.Windows.Forms.Panel
{
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint (e);

Graphics g = e.Graphics;

g.SmoothingMode = SmoothingMode.HighQuality;
GraphicsPath gPath = new GraphicsPath();

Rectangle r = new Rectangle(0,0,this.Width,this.Height);
gPath.AddRectangle(r);

LinearGradientBrush lb = new LinearGradientBrush(r,Color.White,Color.Blue,LinearGradientMode.Vertical);

g.FillPath(lb,gPath);
}
}
}

Cut slice from the left side of picture

// Cut 50 pixels slice from the left side of picture

Bitmap bitmapName= new Bitmap(50, sth, PixelFormat.sth);
Graphics g = Graphics.FromImage(bitmapName);
g.DrawImageUnscaled(OriginalBitmap,....);
g.dispose();

Cut part of the image file

// Cut part of the image file

// --------
// Method 1
// --------

Rectangle rect = new Rectangle(myImage1.Left, myImage1.Top, myImage1.Width - 200, myImage1.Height - 100);
myImage2 = myImage1.Clone(rect, System.Drawing.Imaging.PixelFormat.Format32bppPArg arg);


// --------
// Method 2
// --------

string path = "C:\\test.jpg";
using (Bitmap orignal = new Bitmap(path))
{
using (Bitmap newimage = new Bitmap((int)(orignal.Width * 0.5), (int)(orignal.Height * 0.5)))
{
using (Graphics newgraphics = Graphics.FromImage(newimage))
{
newgraphics.DrawImage(orignal, 0, 0, new Rectangle(newimage.Width, newimage.Height, orignal.Width - newimage.Width, orignal.Height - newimage.Height), GraphicsUnit.Pixel);
newgraphics.Flush();
}

newimage.Save(new System.IO.FileInfo(path).DirectoryName + "out.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
}
}

Scale Image to desired sizes keeping aspect ratio

// Scale Image to desired sizes keeping aspect ratio

private Image ScaleImage(Image source, int MaxWidth, int MaxHeight)
{
float MaxRatio = MaxWidth / (float) MaxHeight;
float ImgRatio = source.Width / (float) source.Height;

if (source.Width > MaxWidth)
return new Bitmap(source, new Size(MaxWidth, (int) Math.Round(MaxWidth / ImgRatio, 0)));

if (source.Height > MaxHeight)
return new Bitmap(source, new Size((int) Math.Round(MaxWidth * ImgRatio, 0), MaxHeight));

return source;
}

Add border around any control or form

// Add border around any control or form

private static Int32 WS_BORDER = 0x800000;

protected override CreateParams CreateParams
{
get
{
CreateParams parameters = base.CreateParams;
parameters.Style |= WS_BORDER;

return parameters;
}
}