|
Terimakasih sekali!
very good stuff
|
|
|
|
|
very nice work, but some problem encountered.
there is a problem when the form is set to transparent mode.
kindly fix Thanks
unleash the possibilities
|
|
|
|
|
A really very good job ,thanks
|
|
|
|
|
After being a lurker here for a while, and getting many good ideas, like "Iconits", Here is a small contribution. A partial update to the original code, which I think helps clarify what it does, and simplifies it.
When I found Iconits, I thought it was neat, but had several issues with it, which are improved here; 1) The alpha shading didn't seem right, and was really slow and/or unsafe. You can delete the whole "Alpha.cs" business now. 2) Names of properties improved to be more meaningful. 3) System.Component model attributes added to the properties to group them together in the properties pane (and dropped "Blur" - why would you use that?). 4) Started renaming other things to be accurate - like "Icon", which is now "Image" (and the whole class name, for that matter)...
Thanks to the original poster, and all who added to it over the months. I found it in Jan '07 and thought I would post this update (about half-way along its transformation to becoming "production code") for all to enjoy!
This class is still not polished, but I wanted to post it while there was still enough of it left to be recognized as "Iconits".
Anyhow, put this class in your project, drop one on your form, set an icon and some sizes and you will see it does the same thing Icontits does.
<br />
<br />
using System;<br />
using System.Collections;<br />
using System.ComponentModel;<br />
using System.Drawing;<br />
using System.Windows.Forms;<br />
using System.Drawing.Drawing2D;<br />
using System.Drawing.Imaging;<br />
<br />
namespace UsedToBeIconits<br />
{<br />
public partial class PuffyImage : UserControl<br />
{<br />
private System.Windows.Forms.Timer drawingTimer;<br />
private System.Windows.Forms.ToolTip t = new ToolTip();<br />
<br />
private Bitmap bitmap;<br />
private Bitmap dblbuffer;<br />
private Graphics dbufGraphics;<br />
<br />
private int imwidth, imheight;<br />
private double curwidth, curheight;<br />
private double addx, addy;<br />
<br />
private int flag;<br />
private bool enter;<br />
<br />
static float[][] _matrixItemsActive ={ <br />
new float[] {1, 0, 0, 0, 0},<br />
new float[] {0, 1, 0, 0, 0},<br />
new float[] {0, 0, 1, 0, 0},<br />
new float[] {0, 0, 0, 1.00f, 0},
new float[] {0, 0, 0, 0, 1}<br />
};<br />
static float[][] _matrixItemsDim ={ <br />
new float[] {1, 0, 0, 0, 0},<br />
new float[] {0, 1, 0, 0, 0},<br />
new float[] {0, 0, 1, 0, 0},<br />
new float[] {0, 0, 0, 0.75f, 0},
new float[] {0, 0, 0, 0, 1}<br />
};<br />
static float[][] _matrixItemsDimmer ={ <br />
new float[] {1, 0, 0, 0, 0},<br />
new float[] {0, 1, 0, 0, 0},<br />
new float[] {0, 0, 1, 0, 0},<br />
new float[] {0, 0, 0, 0.50f, 0},
new float[] {0, 0, 0, 0, 1}<br />
};<br />
static float[][] _matrixItemsDimmest ={ <br />
new float[] {1, 0, 0, 0, 0},<br />
new float[] {0, 1, 0, 0, 0},<br />
new float[] {0, 0, 1, 0, 0},<br />
new float[] {0, 0, 0, 0.25f, 0},
new float[] {0, 0, 0, 0, 1}<br />
};<br />
<br />
ColorMatrix[] _colorMatrix = {<br />
new ColorMatrix(_matrixItemsActive),<br />
new ColorMatrix(_matrixItemsDim),<br />
new ColorMatrix(_matrixItemsDimmer),<br />
new ColorMatrix(_matrixItemsDimmest)<br />
};<br />
<br />
ImageAttributes[] _imageAttributes;<br />
<br />
public PuffyImage()<br />
{<br />
_imageAttributes = new ImageAttributes[4];<br />
for (int ii = 0; ii < 4; ii++)<br />
{<br />
_imageAttributes[ii] = new ImageAttributes();<br />
<br />
_imageAttributes[ii].SetColorMatrix(<br />
_colorMatrix[ii],<br />
ColorMatrixFlag.Default,<br />
ColorAdjustType.Bitmap);<br />
}<br />
<br />
dblbuffer = new Bitmap(Width, Height, PixelFormat.Format32bppArgb);<br />
dbufGraphics = Graphics.FromImage(dblbuffer);<br />
<br />
SizeWhenIdle = new Size(Width / 2, Height / 2);<br />
<br />
InitializeComponent();<br />
<br />
this.drawingTimer = new System.Windows.Forms.Timer(this.components);<br />
<br />
this.drawingTimer.Interval = 10;<br />
this.drawingTimer.Tick += new System.EventHandler(this.timer1_Tick);<br />
<br />
this.BackColor = System.Drawing.SystemColors.Control;<br />
this.Name = "BalloonButton";<br />
this.SizeWhenActive = new System.Drawing.Size(160, 128);<br />
this.Paint += new System.Windows.Forms.PaintEventHandler(this.Iconits_Paint);<br />
this.MouseEnter += new System.EventHandler(this.Iconits_MouseEnter);<br />
this.MouseLeave += new System.EventHandler(this.Iconits_MouseLeave);<br />
}<br />
<br />
private void Iconits_Paint(object sender, System.Windows.Forms.PaintEventArgs e)<br />
{<br />
draw(3);<br />
}<br />
<br />
private void calc()<br />
{<br />
curwidth = imwidth;<br />
curheight = imheight;<br />
<br />
addx = (double)(Width - imwidth) / 10;<br />
addy = (double)(Height - imheight) / 10;<br />
}<br />
<br />
private void draw(int state)<br />
{<br />
int st;
<br />
st = state;<br />
<br />
<br />
if (bitmap == null)<br />
return;<br />
<br />
dbufGraphics.Clear(((this.Parent == null) || (this.BackColor != Color.Transparent)) ? this.BackColor : this.Parent.BackColor);<br />
<br />
Rectangle rb = new Rectangle(<br />
(int)((double)Width - curwidth) / 2,<br />
(int)((double)Height - curheight) / 2,<br />
(int)curwidth,<br />
(int)curheight);<br />
<br />
<br />
dbufGraphics.DrawImage(<br />
bitmap,<br />
rb,
0.0f,
0.0f,
bitmap.Width,
bitmap.Height,
GraphicsUnit.Pixel,<br />
_imageAttributes[st]);<br />
<br />
<br />
#if (DEBUG)<br />
dbufGraphics.DrawRectangle(Pens.Goldenrod, rb);
#endif<br />
<br />
using (Graphics g = this.CreateGraphics())<br />
{<br />
g.DrawImageUnscaled(dblbuffer, 0, 0);<br />
#if (DEBUG)<br />
Rectangle r = this.ClientRectangle;<br />
r.Inflate(-1, -1);<br />
g.DrawRectangle(System.Drawing.Pens.Green, r);
#endif<br />
}<br />
}<br />
<br />
private void timer1_Tick(object sender, System.EventArgs e)<br />
{<br />
if (enter)<br />
{<br />
if (curwidth < Width)<br />
curwidth += addx;<br />
<br />
if (curheight < Height)<br />
curheight += addy;<br />
<br />
if (curwidth >= Width && curheight >= Height)<br />
drawingTimer.Enabled = false;<br />
<br />
flag++;<br />
}<br />
else<br />
{<br />
if (curwidth > imwidth)<br />
curwidth -= addx;<br />
<br />
if (curheight > imheight)<br />
curheight -= addy;<br />
<br />
if (curwidth <= imwidth && curheight <= imheight)<br />
drawingTimer.Enabled = false;<br />
<br />
flag--;<br />
}<br />
<br />
if (flag > 9)<br />
draw(0);<br />
else if (flag > 6)<br />
draw(1);<br />
else if (flag > 3)<br />
draw(2);<br />
else<br />
draw(3);<br />
}<br />
<br />
private void Iconits_MouseEnter(object sender, System.EventArgs e)<br />
{<br />
enter = true;<br />
drawingTimer.Enabled = true;<br />
}<br />
<br />
private void Iconits_MouseLeave(object sender, System.EventArgs e)<br />
{<br />
enter = false;<br />
drawingTimer.Enabled = true;<br />
}<br />
<br />
[Category("Settings")]<br />
[Description("Basic image for your Iconit")]<br />
public Bitmap Image<br />
{<br />
get { return bitmap; }<br />
set<br />
{<br />
bitmap = value;<br />
<br />
calc();<br />
draw(3);<br />
}<br />
}<br />
<br />
[Category("Settings")]<br />
[Description("Maximum size for your image (set to the native size)")]<br />
public new Size SizeWhenActive<br />
{<br />
get { return new Size(Width, Height); }<br />
set<br />
{<br />
Width = ((Size)value).Width;<br />
Height = ((Size)value).Height;<br />
calc();<br />
draw(3);<br />
}<br />
}<br />
<br />
[Category("Settings")]<br />
[Description("Minimum size for your image (when it is idle and dim)")]<br />
public Size SizeWhenIdle<br />
{<br />
get { return new Size(imwidth, imheight); }<br />
set<br />
{<br />
imwidth = ((Size)value).Width;<br />
imheight = ((Size)value).Height;<br />
if (imwidth > Width)<br />
imwidth = Width;<br />
if (imheight > Height)<br />
imheight = Height;<br />
calc();<br />
draw(3);<br />
}<br />
}<br />
<br />
public string TooltipText<br />
{<br />
get { return t.GetToolTip(this); }<br />
set { t.SetToolTip(this, value); }<br />
}<br />
<br />
protected override void OnVisibleChanged(EventArgs e)<br />
{<br />
base.OnVisibleChanged(e);<br />
if (this.Visible == true)<br />
draw(3);<br />
}<br />
<br />
}
<br />
}
|
|
|
|
|
Blackholeat:
You may want to add the following line to the constructor:
this.components = new System.ComponentModel.Container();
Otherwise, "Anyhow, put this class in your project, drop one on your form, set an icon and some sizes and you will see it does the same thing Icontits does." can be taken literally. You can only use one Iconit(puffy image) control without the above line.
Thanks for the simplification.
|
|
|
|
|
What is the optimal icon one should use? size and type (gif?, etc)
thanks
Ralph
|
|
|
|
|
Probably basic - I figure that the icons are in the resource file but how does view them, add more, change them, etc?
thanks
Ralph
|
|
|
|
|
thank you for this code
cood luck ;P
|
|
|
|
|
Please find updated source for Alpha.cs/Iconit.cs. I fixed most of the logical oddities, included all interresting patches/fixes found in others posts. I apologize because I didn't explicitely surrounded everyone contribution with its author name.
Notice the #if (DEBUG) that draws black boxes around bitmaps. That helps to show what happens when the component is drawn at runtime.
Don't define DEBUG or remove code between #if DEBUG / #endif to get rid of black boxes.
Hope you will find this useful !
using System;
using System.Drawing;
using System.Drawing.Imaging;
namespace ITS.Windows.Forms
{
public class Alpha
{
private static int min(int val)
{
return (val<0?0:val);
}
public static void setAlpha(Bitmap bmp, int alpha)
{
Color col;
for (int i=0;i<bmp.Width;i++)
for (int j=0;j<bmp.Height;j++)
{
col=bmp.GetPixel(i,j);
if (col.A>0)
bmp.SetPixel(i,j,Color.FromArgb(min(col.A-alpha),col.R,col.G,col.B));
}
}
public static void returnAlpha2(ref Bitmap bmp, int alpha, ref Bitmap bmpModify)
{
Color col;
for (int i=0;i<bmp.Width;i++)
for (int j=0;j<bmp.Height;j++)
{
col=bmp.GetPixel(i,j);
if (col.A>0)
bmpModify.SetPixel(i,j,Color.FromArgb(min(col.A-alpha),col.R,col.G,col.B));
}
}
public static unsafe void returnAlpha(ref Bitmap bmp, int alpha, ref Bitmap modifiedBmp)
{
if (bmp.PixelFormat != PixelFormat.Format24bppRgb
&& bmp.PixelFormat != PixelFormat.Format32bppArgb)
throw new ArgumentException();
GraphicsUnit gu = GraphicsUnit.Pixel;
RectangleF rectF = bmp.GetBounds(ref gu);
Rectangle rect = Rectangle.Round(rectF);
BitmapData bmpData = bmp.LockBits(rect, ImageLockMode.ReadOnly, bmp.PixelFormat);
rectF = modifiedBmp.GetBounds(ref gu);
rect = Rectangle.Round(rectF);
BitmapData copyData = modifiedBmp.LockBits(rect,
ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
byte* oPx = (byte*)bmpData.Scan0;
byte* cPx = (byte*)copyData.Scan0;
byte b, a;
for (int y=0; y<bmpData.Height; y++)
{
for (int x=0; x<bmpData.Width; x++)
{
*cPx = *oPx;
*(cPx+1) = *(oPx+1);
*(cPx+2) = *(oPx+2);
if (bmpData.PixelFormat == PixelFormat.Format32bppArgb)
{
b = *(oPx+3);
}
else
{
b = (byte)((*cPx + *(cPx+1) + *(cPx+2)) / 3);
}
a = 0;
if (b > 0)
{
a = (byte)Math.Min(255, Math.Max(0, b-alpha));
}
*(cPx+3) = a;
cPx += 4;
if (bmpData.PixelFormat == PixelFormat.Format24bppRgb)
oPx += 3;
else
oPx += 4;
}
if (bmpData.PixelFormat == PixelFormat.Format24bppRgb)
oPx += bmpData.Width;
}
modifiedBmp.UnlockBits(copyData);
bmp.UnlockBits(bmpData);
}
}
}
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
namespace ITS.Windows.Forms
{
public class Iconits : System.Windows.Forms.UserControl
{
private System.ComponentModel.IContainer components;
private System.Windows.Forms.Timer timer1;
private System.Windows.Forms.ToolTip t=new ToolTip();
private Bitmap[] bmp;
private Bitmap dblbuffer;
private Graphics dbufGraphics;
private int imwidth,imheight;
private double curwidth,curheight;
private double addx,addy;
private int flag;
private bool enter;
private bool blur = true;
public Iconits()
{
bmp = new Bitmap[4];
dblbuffer = new Bitmap(Width,Height,PixelFormat.Format32bppArgb);
dbufGraphics = Graphics.FromImage(dblbuffer);
IconSize = new Size(Width/2,Height/2);
InitializeComponent();
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Component Designer generated code
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.timer1 = new System.Windows.Forms.Timer(this.components);
this.timer1.Interval = 10;
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
this.BackColor = System.Drawing.SystemColors.Control;
this.Name = "Iconits";
this.Size = new System.Drawing.Size(160, 128);
this.Paint += new System.Windows.Forms.PaintEventHandler(this.Iconits_Paint);
this.MouseEnter += new System.EventHandler(this.Iconits_MouseEnter);
this.MouseLeave += new System.EventHandler(this.Iconits_MouseLeave);
}
#endregion
private void Iconits_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
draw(3);
}
private void calc()
{
curwidth = imwidth;
curheight = imheight;
addx = (double)(Width-imwidth)/10;
addy = (double)(Height-imheight)/10;
}
private void draw(int state)
{
int st;
if (blur)
st=state;
else
st=0;
if (bmp[st] == null)
return;
dbufGraphics.Clear(((this.Parent == null) || (this.BackColor != Color.Transparent)) ? this.BackColor : this.Parent.BackColor);
Rectangle rb = new Rectangle(
(int)((double)Width-curwidth)/2,
(int)((double)Height-curheight)/2,
(int)curwidth,
(int)curheight);
dbufGraphics.DrawImage(bmp[st], rb);
#if (DEBUG)
dbufGraphics.DrawRectangle(Pens.Black, rb);
#endif
using (Graphics g = this.CreateGraphics())
{
g.DrawImageUnscaled(dblbuffer,0,0);
#if (DEBUG)
Rectangle r = this.ClientRectangle;
r.Inflate(-1,-1);
g.DrawRectangle(System.Drawing.Pens.Black,r);
#endif
}
}
private void timer1_Tick(object sender, System.EventArgs e)
{
if (enter)
{
if (curwidth < Width)
curwidth += addx;
if (curheight < Height)
curheight += addy;
if (curwidth >= Width && curheight >= Height)
timer1.Enabled = false;
flag++;
}
else
{
if (curwidth > imwidth)
curwidth -= addx;
if (curheight > imheight)
curheight -= addy;
if (curwidth <= imwidth && curheight <= imheight)
timer1.Enabled = false;
flag--;
}
if (flag>9)
draw(0);
else if (flag>6)
draw(1);
else if (flag>3)
draw(2);
else
draw(3);
}
private void Iconits_MouseEnter(object sender, System.EventArgs e)
{
enter = true;
timer1.Enabled = true;
}
private void Iconits_MouseLeave(object sender, System.EventArgs e)
{
enter = false;
timer1.Enabled = true;
}
public Bitmap Icon
{
get { return bmp[0]; }
set {
bmp[0]=value;
for (int i = 1 ; i < 4 ; i++)
{
if (bmp[i] != null)
bmp[i].Dispose();
bmp[i] = new Bitmap(bmp[0].Width,bmp[0].Height,PixelFormat.Format32bppArgb);
}
if (bmp[0] != null)
{
Alpha.returnAlpha(ref bmp[0], 60, ref bmp[1]);
Alpha.returnAlpha(ref bmp[0], 120, ref bmp[2]);
Alpha.returnAlpha(ref bmp[0], 180, ref bmp[3]);
}
calc();
draw(3);
}
}
public new Size Size
{
get { return new Size(Width,Height); }
set
{
Width = ((Size)value).Width;
Height = ((Size)value).Height;
calc();
draw(3);
}
}
public Size IconSize
{
get { return new Size(imwidth,imheight); }
set
{
imwidth = ((Size)value).Width;
imheight = ((Size)value).Height;
if (imwidth > Width)
imwidth = Width;
if (imheight > Height)
imheight = Height;
calc();
draw(3);
}
}
public bool Blur
{
get { return blur; }
set
{
blur=value;
if (!blur)
{
bmp[1].Dispose();
bmp[2].Dispose();
bmp[3].Dispose();
}
else
{
for (int i = 1 ; i < 4 ; i++)
{
if (bmp[i] != null)
bmp[i].Dispose();
bmp[i] = new Bitmap(Width,Height,PixelFormat.Format32bppArgb);
}
if (bmp[0] != null)
{
Alpha.returnAlpha(ref bmp[0], 60, ref bmp[1]);
Alpha.returnAlpha(ref bmp[0], 120, ref bmp[2]);
Alpha.returnAlpha(ref bmp[0], 180, ref bmp[3]);
}
}
}
}
public string About
{
get { return "Iconits 0.1.1 - ITS Informatics Surabaya, author: Yojana Hanif"; }
}
public string TooltipText
{
get { return t.GetToolTip(this); }
set { t.SetToolTip(this,value); }
}
protected override void OnVisibleChanged(EventArgs e)
{
base.OnVisibleChanged(e);
if (this.Visible == true)
draw(3);
}
}
}
|
|
|
|
|
Hello Lehlu,
All of your 'for' loops in Alpha.cs are incomplete! Just thought I'd let you know, seems like they've been cut off.
Cheers,
-M
|
|
|
|
|
Hi all,
it seems that the application still uses more and more memory if one moves the mouse over the icons. Some threads below there is an advice in Thread 'Very nice but...' but it seems it's not added to current solution. It would be great if someone could be so kind to post the updated code here. Many thanks!
|
|
|
|
|
|
Great job! Exactly what I was looking for! Many thanks!!
Just one short idea: When the icons are small there is much free space between the icons. Would it be possible to dynamically create the free space as soon as the user moves the mouse over an icon? So if there is no mouse pointer at the icon, all icons are next to each other in 32x32. As soon as the mouse points on one icon, all other icons are moved to the left and right so there is enough space to display the bigger icon. Would this be possible?
Nevertheless 5 points for this brillant piece of code!
|
|
|
|
|
There was a little bug, after hide/show of the parent forms which contains the iconit, the iconit wasnt redrawed and it generated a GDI+ exception.
Adding this override to Iconits.cs will resolve the problem
protected override void OnVisibleChanged(EventArgs e)
{
base.OnVisibleChanged(e);
if (this.Visible == true)
{
g = this.CreateGraphics();
draw(3);
}
}
-- Everything is possible, even the impossible! ^_^
|
|
|
|
|
Hi,
First, this simple control is really nice.
Ok, if you change the BackColor of a Iconit to, let's say, Transparent, because you want it to appear with its parent backcolor, when you move the mouse in and out the control, all of the bitmaps displayed during the process remain on screen. If you minimize the window or switch to another and back, the display is ok again.
I fixed this with a simple change in this line:
private void draw(int state)
{
...
// OLD LINE: g2.Clear(this.BackColor);
g2.Clear(((this.Parent == null) || (this.BackColor != Color.Transparent)) ? this.BackColor : this.Parent.BackColor);
...
}
This way, if you want your Iconit to inherit its parent backcolor just set the Iconit's backcolor to Transparent. You won't have to change backcolor for every Iconit if you decide to change your container backcolor (e.g. a Form). In case you need your Iconit with a different backcolor than the container's, well, just set the backcolor for your iconit.
Have a nice day.
|
|
|
|
|
If I use this component in the forms which is not rectangle( set the TransparencyKey) , it can't work properly .
How can I fix this problem ?
|
|
|
|
|
Read my "Bug + Fix: BackColor problem" post in this thread.
|
|
|
|
|
Thanks ~~~~~
|
|
|
|
|
Hi Hanifku,
Really enjoyed this, and I hope we'll see a new version soon incorporating the comments on speeding it up.
best, Bill
"The greater the social and cultural distances between people, the more magical the light that can spring from their contact." Milan Kundera in Testaments Trahis
|
|
|
|
|
I think this piece of code is great. I just found the following behavior which is kind of interesting:
When you hover the mouse and the icon is resized and is showing full color and you click or double click, nothing happens. Good.
If you do the same, wait until the tooltip shows up, and then click or double click, the icon loses its color.
Anyway, this is a really cool control. Congrats!!!
A.G
|
|
|
|
|
Hi,the click event handler you need to add in by each of it for your iconit button.
inside the windows Designer generate form code, each iconits button you use need to add. let say..
this.iconits7.Click += new System.EventHandler(this.iconits_Click);<br />
<br />
private void iconits_Click(object sender, System.EventArgs e)<br />
{<br />
<br />
}
I do realize that, but nothing affect much for me since if user click already, The other's action they concentrate on....
|
|
|
|
|
What I noticed is that the Tooltip stops showing after the first time you click on the Iconit. Once clicked, the tooltip won't show up again.
Ideas?
|
|
|
|
|
I noticed your routeen to update the alpha of the bitmap is using a slower method (actually, about the slowest there is). Here is a significantly faster version of your routeen. It uses an unsafe code function, which allows the use of pointers. This function will adjust the original alpha by the input alpha amount. Since it uses pointers to directly edit the memory of the copied bitmap, it is hundreds of times faster than using the GetPixel/SetPixel method of GDI+. In fact, GDI+ essentially does the first and last parts of my function (locking pixels, finding the pixel in memory, reading/writing it, and unlocking), every time you call GetPixel or SetPixel. By using an unsafe function with pointers, you eliminate the repetitive locking/seeking/unlocking of pixel data in memory, which greatly improves performance.
public unsafe Bitmap returnAlpha(Bitmap bmp, int alpha)
{
if (bmp.PixelFormat != PixelFormat.Format24bppRgb
&& bmp.PixelFormat != PixelFormat.Format32bppArgb)
throw new ArgumentException();
Bitmap bmpCopy = new Bitmap(bmp.Width, bmp.Height, PixelFormat.32bppArgb);
GraphicsUnit gu = GraphicsUnit.Pixel;
RectangleF rectF = bmp.GetBounds(ref gu);
Rectangle rect = Rectangle.Round(rectF);
BitmapData bmpData = bmp.LockBits(rect, ImageLockMode.Read, bmp.PixelFormat);
rectF = bmpCopy.GetBounds(ref gu);
rect = Rectangle.Round(rectF);
BitmapData copyData = bmpCopy.LockBits(rect, ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
byte* oPx = (byte*)bmpData.Scan0;
byte* cPx = (byte*)copyData.Scan0;
uint black = 255;
byte b, a;
for (int y=0; y<bmpData.Height; y++)
{
for (int x=0; x<bmpData.Width; x++)
{
*cPx = *oPx;
*(cPx+1) = *(oPx+1);
*(cPx+2) = *(oPx+2);
if (bmpData.PixelFormat == PixelFormat.Format32bppArgb)
{
b = *(oPx+3);
}
else
{
b = (byte)((*cPx + *(cPx+1) + *(cPx+2)) / 3);
}
a = 0;
if (b > 0)
{
a = (byte)Math.Min(255, Math.Max(0, b-alpha));
}
*(cPx+3) = a;
cPx += 4;
if (bmpData.PixelFormat == PixelFormat.Format24bppRgb)
oPx += 3;
else
oPx += 4;
}
if (bmpData.PixelFormat == PixelFormat.Format24bppRgb)
oPx += bmpData.Width;
}
copyBmp.UnlockBits(copyData);
bmp.UnlockBits(bmpData);
return copyBmp;
}
-- modified at 23:23 Thursday 20th April, 2006
|
|
|
|
|
For some reason, CodeProject is stripping whitespace from my code. Sorry for it being all packed together.
|
|
|
|
|
I've tested your method, it's fanstastic.
Just in case people donn't, above Jon Rista method need to add
using System.Drawing.Imaging;
******************************************
I alterred the iconits.cs few place to remove those resources killer
namespace alterred<br />
{ <br />
public class Iconits : System.Windows.Forms.UserControl<br />
{<br />
private System.ComponentModel.IContainer components;<br />
private System.Windows.Forms.Timer timer1;<br />
private System.Windows.Forms.ToolTip t=new ToolTip();<br />
Bitmap[] bmp; <br />
int flag;<br />
bool enter;<br />
Graphics g,g2;<br />
<br />
int imwidth,imheight;<br />
double curwidth,curheight;<br />
double addx,addy;<br />
Bitmap dblbuffer;<br />
bool blur=true;<br />
<br />
public Iconits()<br />
{<br />
InitializeComponent();<br />
bmp=new Bitmap[4]; <br />
for (int i=0;i<4;i++)<br />
bmp[i]=new Bitmap(Width,Height);<br />
dblbuffer=new Bitmap(Width,Height);
g2=Graphics.FromImage(dblbuffer); <br />
<br />
IconSize=new Size(Width/2,Height/2); <br />
g=this.CreateGraphics(); <br />
}<br />
<br />
<br />
protected override void Dispose( bool disposing )<br />
{<br />
if( disposing )<br />
{<br />
if(components != null)<br />
{<br />
components.Dispose();<br />
}<br />
}<br />
base.Dispose( disposing );<br />
}<br />
<br />
#region Component Designer generated code<br />
<br />
private void InitializeComponent()<br />
{<br />
this.components = new System.ComponentModel.Container();<br />
this.timer1 = new System.Windows.Forms.Timer(this.components);<br />
this.timer1.Interval = 10;<br />
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);<br />
this.BackColor = System.Drawing.SystemColors.Control;<br />
this.Name = "Iconits";<br />
this.Size = new System.Drawing.Size(160, 128);<br />
this.Paint += new System.Windows.Forms.PaintEventHandler(this.Iconits_Paint);<br />
this.MouseEnter += new System.EventHandler(this.Iconits_MouseEnter);<br />
this.MouseLeave += new System.EventHandler(this.Iconits_MouseLeave);<br />
<br />
}<br />
#endregion<br />
<br />
private void Iconits_Paint(object sender, System.Windows.Forms.PaintEventArgs e)<br />
{ <br />
<br />
draw(3); <br />
}<br />
<br />
private void calc()<br />
{<br />
curwidth=imwidth;<br />
curheight=imheight;<br />
<br />
addx=(double)(Width-imwidth)/10;<br />
addy=(double)(Height-imheight)/10;<br />
}<br />
<br />
<br />
private void draw(int state)<br />
{<br />
int st;<br />
<br />
if (blur)<br />
st=state;<br />
else<br />
st=0;<br />
<br />
<br />
g2.Clear(this.BackColor);<br />
g2.DrawImage(bmp[st],(int)((double)Width-curwidth)/2,(int)((double)Height-curheight)/2,(int)curwidth,(int)curheight); <br />
g.DrawImageUnscaled(dblbuffer,0,0); }<br />
<br />
private void timer1_Tick(object sender, System.EventArgs e)<br />
{ <br />
if (enter)<br />
{<br />
if (curwidth<Width)<br />
{<br />
curwidth+=addx;<br />
}<br />
<br />
if (curheight<Height)<br />
{<br />
curheight+=addy;<br />
}<br />
<br />
if (curwidth>=Width&&curheight>=Height) timer1.Enabled = false; flag++;<br />
}<br />
else<br />
{<br />
if (curwidth>imwidth)<br />
{<br />
curwidth-=addx;<br />
}<br />
<br />
if (curheight>imheight)<br />
{<br />
curheight-=addy;<br />
}<br />
<br />
if (curwidth<=imwidth&&curheight<=imheight) timer1.Enabled = false;<br />
<br />
flag--;<br />
}<br />
<br />
if (flag>9) draw(0); <br />
else if (flag>6) draw(1);<br />
else if (flag>3) draw(2);<br />
else draw(3); <br />
}<br />
<br />
private void Iconits_MouseEnter(object sender, System.EventArgs e)<br />
{ <br />
enter=true;<br />
timer1.Enabled=true;<br />
}<br />
<br />
private void Iconits_MouseLeave(object sender, System.EventArgs e)<br />
{<br />
enter=false; <br />
timer1.Enabled = true;<br />
}<br />
<br />
public Bitmap Icon<br />
{<br />
get { return bmp[0]; }<br />
set { <br />
<br />
bmp[0]=value; <br />
alpha.returnAlpha3(ref bmp[0],60, ref bmp[1]);
alpha.returnAlpha3(ref bmp[0],120,ref bmp[2]);<br />
alpha.returnAlpha3(ref bmp[0],180,ref bmp[3]); draw(0); <br />
}<br />
}<br />
<br />
public new Size Size<br />
{<br />
get { return new Size(Width,Height); }<br />
set <br />
{<br />
Width = ((Size)value).Width;<br />
Height= ((Size)value).Height; <br />
if (Width>160) <br />
{<br />
Width=160;<br />
}<br />
if (Height>128) <br />
{<br />
Height=128; <br />
}<br />
calc();<br />
}<br />
} <br />
<br />
public Size IconSize<br />
{<br />
get { return new Size(imwidth,imheight); }<br />
set<br />
{<br />
imwidth=((Size)value).Width;<br />
imheight=((Size)value).Height;<br />
if (imwidth>Width) imwidth=Width;<br />
if (imheight>Height) imheight=Height;<br />
calc();<br />
}<br />
}<br />
<br />
public bool Blur<br />
{<br />
get { return blur; }<br />
set<br />
{<br />
blur=value;<br />
if (!blur)<br />
{<br />
bmp[1].Dispose();<br />
bmp[2].Dispose();<br />
bmp[3].Dispose();<br />
}<br />
else<br />
{<br />
alpha.returnAlpha3(ref bmp[0], 60 ,ref bmp[1]);<br />
alpha.returnAlpha3(ref bmp[0], 120 ,ref bmp[2]);<br />
alpha.returnAlpha3(ref bmp[0], 180 ,ref bmp[3]);<br />
<br />
}<br />
}<br />
}<br />
<br />
public string About<br />
{<br />
get { return "Iconits 0.1.0 - ITS Informatics Surabaya, author: Yojana Hanif. Jayoscar Modify Abit"; }<br />
}<br />
<br />
public string TooltipText<br />
{<br />
get { return t.GetToolTip(this); }<br />
set { t.SetToolTip(this,value); <br />
}<br />
}<br />
}<br />
}<br />
<br />
This part on the alpha.cs<br />
public static void returnAlpha2(ref Bitmap bmp, int alpha, ref Bitmap bmpModify)<br />
{<br />
Color col; <br />
<br />
for (int i=0;i<bmp.Width;i++)<br />
for (int j=0;j<bmp.Height;j++) <br />
{<br />
col=bmp.GetPixel(i,j); <br />
if (col.A>0)<br />
bmpModify.SetPixel(i,j,Color.FromArgb(min(col.A-alpha),col.R,col.G,col.B)); <br />
} <br />
}
Well, everyone give some feedback see how this change you feel. Comment/suggestion welcome.
-- modified at 7:02 Friday 21st April, 2006
|
|
|
|
|