Hello,
I am beginner ( Noob ) in c#,
I am trying to make a small Asset browsing tool for myself, I just wanted to be sure if the way i am going is good or is there a better way to do it.
Currently I have 4 classes
ThumbViewer : extends FlowlayoutPanel
ThumbItem : extends Button
CacheManager.
I have overridden the paint method in the ThumbItem class and painting my Own way.
I also have a Background worker process which reads image file and gets thumbnail out if it and then assigns it to the Image property in the Button.
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;
using System.ComponentModel;
using System.Drawing.Imaging;
namespace Thumbnailer
{
public class ThumbItem : Button
{
private BackgroundWorker bw;
private string _fullfilename;
private bool _ismouseover = false;
private bool _isSelected = false;
private const int THUMBNAIL_DATA = 0x501B;
private ToolTip _btn_Tooltip;
private bool isDisposed = false;
private bool _itemvisiblecalled = false;
private bool _itemnotvisiblecalled = false;
#region Properties
public string FullName
{
get { return _fullfilename; }
set { _fullfilename = value; }
}
public bool Selected
{
get { return _isSelected; }
set { _isSelected = value; }
}
public Image FileIcon { get; private set; }
public bool isVisible { get; set; }
public string FileName { get; set; }
#endregion
#region Constructor
public ThumbItem()
{
this.Text = "PlaceHolder Thumb";
this.Size = new Size(128, 144);
this.Padding = new Padding(4);
this.BackColor = Color.Transparent;
this.Font = new Font("Verdana", 9, GraphicsUnit.Point);
this.DoubleBuffered = true;
bw = new BackgroundWorker();
bw.WorkerReportsProgress = true;
bw.WorkerSupportsCancellation = true;
bw.DoWork += new DoWorkEventHandler(BW_DoWork);
bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(BW_RunWorkerCompleted);
}
public ThumbItem(string file)
: this()
{
if (!File.Exists(file))
{
throw new FileNotFoundException("Specified file " + file + " doesnt exists.");
}
FileInfo fi = new FileInfo(file);
this.Text = fi.Name.Replace(fi.Extension, "");
this.FileName = fi.Name.Replace(fi.Extension, "");
this.FullName = fi.FullName;
bw.RunWorkerAsync();
}
~ThumbItem()
{
if(!this.isDisposed)
{
bw.DoWork -= BW_DoWork;
bw.RunWorkerCompleted -= BW_RunWorkerCompleted;
this.bw.Dispose();
}
}
#endregion
#region Background Worker
private void BW_DoWork(object sender, DoWorkEventArgs e)
{
if (this._fullfilename != "")
{
Image _thumbimg = null;
Image _icon = null;
FileInfo fi = new FileInfo(this._fullfilename);
if(fi.Extension == ".JPG")
{
using (MemoryStream ms = new MemoryStream(File.ReadAllBytes(this._fullfilename)))
{
using (Image img = Image.FromStream(ms))
{
if (CacheManager<Image>.CurrentInstance.isExists(this.FullName))
{
_thumbimg = CacheManager<Image>.CurrentInstance.GetByKey(this.FullName);
}
else
{
if (img != null)
{
_thumbimg = img.GetThumbnailImage(64, 64, abort_callback, IntPtr.Zero);
CacheManager<Image>.CurrentInstance.Add(this.FullName, _thumbimg);
}
}
}
}
}
else
{
_icon = Icon.ExtractAssociatedIcon(this.FullName).ToBitmap();
}
e.Result = new Image[] { _thumbimg, _icon };
}
}
private void BW_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
this.Image = ((Image[])e.Result)[0];
this.FileIcon = ((Image[])e.Result)[1];
this.Invalidate();
}
#endregion
#region Painting Methods
protected override void OnPaint(PaintEventArgs pevent)
{
Graphics gfx = pevent.Graphics;
StringFormat formatter = new StringFormat();
formatter.LineAlignment = StringAlignment.Center;
formatter.Alignment = StringAlignment.Center;
Rectangle textrect = new Rectangle(0, (this.Height / 2) - (this.FontHeight / 2) - this.Padding.All, this.Width, this.Height);
Color fontcolor = Color.White;
Rectangle parentbounds = this.Parent.Bounds;
if (this.Parent != null)
{
gfx.Clear(this.Parent.BackColor);
}
if (((this.Bounds.Bottom - 10) < this.Parent.Bounds.Top) || (this.Bounds.Top - 10) > this.Parent.Bounds.Bottom)
{
if(!this._itemnotvisiblecalled)
{
ItemNotVisible();
}
}
else
{
if(!this._itemvisiblecalled)
{
ItemVisible();
}
}
if(this.FileName == "IMG_0035")
{
Console.WriteLine("TEmp");
}
if (this.Selected)
{
gfx.FillRectangle(new LinearGradientBrush(this.ClientRectangle, Color.FromArgb(25, 255, 255, 255), Color.FromArgb(150, 255, 255, 255), LinearGradientMode.Vertical), this.ClientRectangle);
}
if (this._ismouseover)
{
gfx.FillRectangle(new LinearGradientBrush(this.ClientRectangle, Color.FromArgb(25, 255, 255, 255), Color.FromArgb(100, 255, 255, 255), LinearGradientMode.Vertical), this.ClientRectangle);
}
Rectangle borderrect = this.ClientRectangle;
ControlPaint.DrawBorder(gfx, borderrect, Color.FromArgb(100, 255, 255, 255), ButtonBorderStyle.Solid);
if (this.Image != null)
{
Rectangle imgrect = new Rectangle(this.Padding.All, this.Padding.All, (this.Width - (this.Padding.All * 2)), (this.Width - (this.Padding.All * 2)));
gfx.DrawImage(this.Image, imgrect);
}
else if (this.FileIcon != null)
{
Rectangle iconrect = new Rectangle(((this.Width / 2) - 16), ((this.Height / 2) - 16), 32, 32);
gfx.DrawImage(this.FileIcon, iconrect);
}
string temp = this.Text;
SizeF textsize = gfx.MeasureString(temp, this.Font);
while (textsize.Width > this.Width / 1.5)
{
temp = temp.Remove(temp.Length - 1);
textsize = gfx.MeasureString(temp, this.Font);
}
temp += "....";
gfx.DrawString(temp, this.Font, new SolidBrush(fontcolor), textrect, formatter);
}
protected override void OnPaintBackground(PaintEventArgs pevent)
{
base.OnPaintBackground(pevent);
}
#endregion
#region Helper Methods
public static Image ResizeImage(Image imgToResize, Size size)
{
int sourceWidth = imgToResize.Width;
int sourceHeight = imgToResize.Height;
float nPercent = 0;
float nPercentW = 0;
float nPercentH = 0;
nPercentW = ((float)size.Width / (float)sourceWidth);
nPercentH = ((float)size.Height / (float)sourceHeight);
if (nPercentH < nPercentW)
nPercent = nPercentH;
else
nPercent = nPercentW;
int destWidth = (int)(sourceWidth * nPercent);
int destHeight = (int)(sourceHeight * nPercent);
Bitmap b = new Bitmap(destWidth, destHeight);
Graphics g = Graphics.FromImage((Image)b);
g.InterpolationMode = InterpolationMode.Low;
g.DrawImage(imgToResize, 0, 0, destWidth, destHeight);
g.Dispose();
return (Image)b;
}
public void RefreshThumbnail()
{
this.Image = null;
while(!this.bw.IsBusy)
{
this.bw.RunWorkerAsync();
}
}
private void ItemVisible()
{
this._itemvisiblecalled = true;
this._itemnotvisiblecalled = false;
}
private void ItemNotVisible()
{
this._itemvisiblecalled = false;
this._itemnotvisiblecalled = true;
}
private static bool HasJpegHeader(string filename)
{
using (BinaryReader br = new BinaryReader(File.Open(filename, FileMode.Open)))
{
UInt16 soi = br.ReadUInt16();
UInt16 jfif = br.ReadUInt16();
return soi == 0xd8ff && jfif == 0xe0ff;
}
}
#endregion
#region Event Handlers
protected override void OnMouseEnter(EventArgs e)
{
base.OnMouseEnter(e);
this._ismouseover = true;
if ( _btn_Tooltip == null)
{
_btn_Tooltip = new ToolTip();
FileInfo fi = new FileInfo(this.FullName);
string tooltiptext = "";
tooltiptext += "FileName : "+fi.Name+"\n";
tooltiptext += "Path : "+fi.FullName+"\n";
tooltiptext += "Directory : " + fi.DirectoryName + "\n";
_btn_Tooltip.SetToolTip(this, tooltiptext);
}
}
protected override void OnMouseLeave(EventArgs e)
{
base.OnMouseLeave(e);
this._ismouseover = false;
}
protected override void OnClick(EventArgs e)
{
base.OnClick(e);
}
#endregion
#region Event Methods
#endregion
#region UnUsed Methods
private bool abort_callback()
{
return false;
}
#endregion
private void InitializeComponent()
{
this.SuspendLayout();
this.ResumeLayout(false);
}
}
}
Can anyone please tell me if this is the best way to do it, if not then can you please guide me in right direction.
Thanks
Dinesh
|