Saturday, March 7, 2009

SpinningImage in Windows appln

Hi,
We need to create this class file
public partial class ImageRotator
{
Image img=null;
public bool Rotate(RotationType type, string path)
{
try
{

FileStream fs1 = new FileStream(path, FileMode.Open, FileAccess.Read);
img = Image.FromStream(fs1);
img.Save(AppDomain.CurrentDomain.BaseDirectory + "output1.bmp", ImageFormat.Bmp);
fs1.Close();
fs1.Dispose();
// File.Delete(path);
ImageCodecInfo usedIC = this.GetEncoderInfo("image/bmp");
Encoder encoder = Encoder.Quality;
EncoderParameters encparams = new EncoderParameters(1);
EncoderParameter encparam = new EncoderParameter(encoder, (long)100);
encparams.Param[0] = encparam;
Image test = Image.FromFile(AppDomain.CurrentDomain.BaseDirectory + "output1.bmp");
img.Dispose();
img = null;
if (type == RotationType.Rotate90)
test.RotateFlip(RotateFlipType.Rotate90FlipNone);
else if (type == RotationType.Rotate180)
test.RotateFlip(RotateFlipType.Rotate180FlipNone);
else if (type == RotationType.Rotate270)
test.RotateFlip(RotateFlipType.Rotate270FlipNone);
test.Save(AppDomain.CurrentDomain.BaseDirectory + "output1.bmp", usedIC, encparams);
// File.Delete(AppDomain.CurrentDomain.BaseDirectory + "cute1.jpg");
// usedIC = this.GetEncoderInfo("image/jpeg");
test.Save(path, usedIC, encparams);
File.Delete(AppDomain.CurrentDomain.BaseDirectory + "output1.bmp");
GC.Collect();

}

catch { }
{
return false;
}
}
private ImageCodecInfo GetEncoderInfo(string p)
{
ImageCodecInfo[] encoders = ImageCodecInfo.GetImageEncoders();
foreach (ImageCodecInfo codecInfo in encoders)
{
if (codecInfo.MimeType == p)
return codecInfo;
}
return null;
}
}
We need RotationType class file,
public enum RotationType
{
Rotate90,
Rotate180,
Rotate270
}

Create UserControl using this code,
public partial class SpinningImage : UserControl
{
private Boolean completeImage = true;
private static Single angle = 0;
double dw;
double dh;
Image img = null;
Image resizedImage = null;
public SpinningImage()
{
InitializeComponent();
SetStyle(ControlStyles.AllPaintingInWmPaint, true);
SetStyle(ControlStyles.UserPaint, true);
SetStyle(ControlStyles.DoubleBuffer, true);
}
private string imgPath;

public string ImagePath
{
get { return imgPath; }
set { imgPath = value; }
}

public void setImage()
{

//InitializeComponent();
FileStream fs1 = new FileStream(imgPath, FileMode.Open, FileAccess.Read);
img = Image.FromStream(fs1);
fs1.Close();
fs1.Dispose();
dw = pictureBox1.Width;
dh = pictureBox1.Height;
resizedImage = (Image)QDOES.ImageOptimizer.ResizeToMaxSize(img, dw, dh, 96, 96);

}
public void startTimer()
{ if (completeImage == true)
{
if (enableTimer())
{
pictureBox1.Width = (int)dh;
pictureBox1.Height = (int)dw;
dw = pictureBox1.Width;
dh = pictureBox1.Height;

timer1.Start();
timer1.Interval = 10;

pictureBox1.Refresh();
}
}
}

private void timer1_Tick(object sender, EventArgs e)
{



completeImage = false;
// angle = 0;
angle += 1;

if ((angle >= 0) && (angle < 90))
{
angle++;
}
else if ((angle >= 90) && (angle < 180))
{
if (angle < 180)
angle++;
}
else if ((angle >= 180) && (angle < 270))
{
if (angle < 270)
angle++;
}
else if ((angle >= 270) && (angle < 360))
{
if (angle < 360)
angle++;
}
if (angle == 90)
{
timer1.Stop();completeImage = true;
SaveImage();
}
else if (angle == 180)
{
timer1.Stop();completeImage = true;
SaveImage();
}
else if (angle == 270)
{
timer1.Stop();completeImage = true;
SaveImage();
}
else if (angle == 360)
{
SaveImage();
timer1.Stop(); completeImage = true;
angle = 0;
}
pictureBox1.Invalidate();
}

public bool enableTimer()
{
//string inputPath = AppDomain.CurrentDomain.BaseDirectory + "cute1.jpg";
timer1.Enabled = true;
//timer1.Start();
/* ImageRotator ir = new ImageRotator();
if (String.IsNullOrEmpty(imgPath))
throw new ArgumentException("Invalid input image path.");
if (!File.Exists(imgPath))
throw new FileNotFoundException("Input file not find.");
if (imgPath != null)
{
return ir.Rotate(RotationType.Rotate90, imgPath);
}*/
return false;
}
public void SaveImage()
{
ImageRotator ir = new ImageRotator();
if (String.IsNullOrEmpty(imgPath))
throw new ArgumentException("Invalid input image path.");
if (!File.Exists(imgPath))
throw new FileNotFoundException("Input file not find.");
if (imgPath != null)
{
ir.Rotate(RotationType.Rotate90, imgPath);
}
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
//resizedImage = (Image)QDOES.ImageOptimizer.ResizeToMaxSize(img, dw, dh, 96, 96);
if(resizedImage !=null)
{
Bitmap bm = new Bitmap((int)dw, (int)dh);
Graphics g = Graphics.FromImage((Image)bm);
g.DrawImage(resizedImage, new Rectangle((bm.Width - resizedImage.Width) / 2, (bm.Height - resizedImage.Height) / 2, resizedImage.Width, resizedImage.Height), new Rectangle(0, 0, resizedImage.Width, resizedImage.Height), GraphicsUnit.Pixel);
resizedImage = (Image)bm;
TextureBrush tex = new TextureBrush(resizedImage, WrapMode.Clamp);
float x = (float)dw / 2;
float y = (float)dh / 2;
tex.TranslateTransform(-x, -y);
tex.RotateTransform(angle, MatrixOrder.Append);
tex.TranslateTransform(x, y, MatrixOrder.Append);
e.Graphics.FillRectangle(tex, e.ClipRectangle);
}
}
}


This will give u the spinning effect