我继承了button 用Image.fromfile 加载图片 再 加到imageList 中 然后再 用Graphics 画上
就会发现图片显示的质量有问题using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
namespace TestButton
{
public partial class UserControl1 : System.Windows.Forms.Panel
{
private string none ; //什么都没有的情况
private string hover; // 鼠标 划过 悬停的时候路径
private string click ; //鼠标电击时候的图片路径
private Image image;
private int ww;
private int hh;
private bool ok;
private bool error;
private LinearGradientBrush linGrBrush;
private bool showfont;
public UserControl1()
{
this.MouseDown += new MouseEventHandler(UserControl1_MouseDown);
this.MouseHover += new EventHandler(UserControl1_MouseHover);
this.MouseLeave += new EventHandler(UserControl1_MouseLeave);
this.MouseUp += new MouseEventHandler(UserControl1_MouseUp);
this.MouseEnter += new EventHandler(UserControl1_MouseEnter);
InitializeComponent();
} private void tests()
{
string nones = Application.StartupPath + "\\" + this.none;
string clicks = Application.StartupPath + "\\" + this.click;
string hovers = Application.StartupPath + "\\" + this.hover;
try
{
Image im1 = Image.FromFile(nones,false);
Image im2 = Image.FromFile(hovers, false);
Image im3 = Image.FromFile(clicks, false);
this.ww = im1.Width;
this.hh = im1.Height;
Size s = new Size();
s.Width = this.ww;
s.Height = this.hh;
this.imageList1.ImageSize = s;
this.imageList1.Images.AddRange(new Image[] { im1, im2, im3 }); }catch(Exception e){
this.error = true;
}
} protected override void OnPaint(PaintEventArgs pevent)
{
base.OnPaint(pevent);
if (!this.ok)
{
this.tests();
linGrBrush = new LinearGradientBrush(new Point(0, 0), new Point(0, 100), Color.White, Color.White);
this.ok = true;
}
if(this.image == null)
this.image = this.imageList1.Images[0]; if (this.error)
base.OnPaint(pevent);
else
{
this.ForeColor = Color.White;
Graphics e = pevent.Graphics;
int startx = pevent.ClipRectangle.X;
int starty = pevent.ClipRectangle.Y; this.Width = this.ww;
this.Height = this.hh;
SizeF f = e.MeasureString(Text, Font);
float w = f.Width;
float h = f.Height;
w = (Size.Width - w) / 2;
h = (Size.Height - h) / 2;
this.BackgroundImage = this.image;
//EncoderParameter ep = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality,100L);
//ImageCodecInfo info = new ImageCodecInfo();
//info.CodecName;
// this.image.GetEncoderParameterList(System.Drawing.Imaging.Encoder.Quality);
// e.DrawImage(this.image, new Point(startx, starty));
if(this.showfont)
e.DrawString(Text, Font, linGrBrush, w, h);
}
} public bool Showfont
{
get { return showfont; }
set { showfont = value; }
} public string None
{
get { return none; }
set { none = value; }
} public string Hover
{
get { return hover; }
set { hover = value; }
} public string Click1
{
get { return click; }
set { click = value; }
} void UserControl1_MouseEnter(object sender, EventArgs e)
{
if (!this.error)
{
this.image = this.imageList1.Images[1];
this.Invalidate();
}
} void UserControl1_MouseUp(object sender, MouseEventArgs e)
{
if (!this.error)
{
this.image = this.imageList1.Images[1];
linGrBrush = new LinearGradientBrush(new Point(0, 0), new Point(0, 100), Color.White, Color.White);
this.Invalidate();
}
} void UserControl1_MouseLeave(object sender, EventArgs e)
{
if (!this.error)
{
this.image = this.imageList1.Images[0];
this.Invalidate();
}
} void UserControl1_MouseHover(object sender, EventArgs e)
{
if (!this.error)
{
this.image = this.imageList1.Images[1];
this.Invalidate();
}
} void UserControl1_MouseDown(object sender, MouseEventArgs e)
{
if (!this.error)
{
this.image = this.imageList1.Images[2];
this.linGrBrush = new LinearGradientBrush(new Point(0, 0), new Point(0, 100), Color.Black, Color.Black);
this.Invalidate();
}
} }
}
就会发现图片显示的质量有问题using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
namespace TestButton
{
public partial class UserControl1 : System.Windows.Forms.Panel
{
private string none ; //什么都没有的情况
private string hover; // 鼠标 划过 悬停的时候路径
private string click ; //鼠标电击时候的图片路径
private Image image;
private int ww;
private int hh;
private bool ok;
private bool error;
private LinearGradientBrush linGrBrush;
private bool showfont;
public UserControl1()
{
this.MouseDown += new MouseEventHandler(UserControl1_MouseDown);
this.MouseHover += new EventHandler(UserControl1_MouseHover);
this.MouseLeave += new EventHandler(UserControl1_MouseLeave);
this.MouseUp += new MouseEventHandler(UserControl1_MouseUp);
this.MouseEnter += new EventHandler(UserControl1_MouseEnter);
InitializeComponent();
} private void tests()
{
string nones = Application.StartupPath + "\\" + this.none;
string clicks = Application.StartupPath + "\\" + this.click;
string hovers = Application.StartupPath + "\\" + this.hover;
try
{
Image im1 = Image.FromFile(nones,false);
Image im2 = Image.FromFile(hovers, false);
Image im3 = Image.FromFile(clicks, false);
this.ww = im1.Width;
this.hh = im1.Height;
Size s = new Size();
s.Width = this.ww;
s.Height = this.hh;
this.imageList1.ImageSize = s;
this.imageList1.Images.AddRange(new Image[] { im1, im2, im3 }); }catch(Exception e){
this.error = true;
}
} protected override void OnPaint(PaintEventArgs pevent)
{
base.OnPaint(pevent);
if (!this.ok)
{
this.tests();
linGrBrush = new LinearGradientBrush(new Point(0, 0), new Point(0, 100), Color.White, Color.White);
this.ok = true;
}
if(this.image == null)
this.image = this.imageList1.Images[0]; if (this.error)
base.OnPaint(pevent);
else
{
this.ForeColor = Color.White;
Graphics e = pevent.Graphics;
int startx = pevent.ClipRectangle.X;
int starty = pevent.ClipRectangle.Y; this.Width = this.ww;
this.Height = this.hh;
SizeF f = e.MeasureString(Text, Font);
float w = f.Width;
float h = f.Height;
w = (Size.Width - w) / 2;
h = (Size.Height - h) / 2;
this.BackgroundImage = this.image;
//EncoderParameter ep = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality,100L);
//ImageCodecInfo info = new ImageCodecInfo();
//info.CodecName;
// this.image.GetEncoderParameterList(System.Drawing.Imaging.Encoder.Quality);
// e.DrawImage(this.image, new Point(startx, starty));
if(this.showfont)
e.DrawString(Text, Font, linGrBrush, w, h);
}
} public bool Showfont
{
get { return showfont; }
set { showfont = value; }
} public string None
{
get { return none; }
set { none = value; }
} public string Hover
{
get { return hover; }
set { hover = value; }
} public string Click1
{
get { return click; }
set { click = value; }
} void UserControl1_MouseEnter(object sender, EventArgs e)
{
if (!this.error)
{
this.image = this.imageList1.Images[1];
this.Invalidate();
}
} void UserControl1_MouseUp(object sender, MouseEventArgs e)
{
if (!this.error)
{
this.image = this.imageList1.Images[1];
linGrBrush = new LinearGradientBrush(new Point(0, 0), new Point(0, 100), Color.White, Color.White);
this.Invalidate();
}
} void UserControl1_MouseLeave(object sender, EventArgs e)
{
if (!this.error)
{
this.image = this.imageList1.Images[0];
this.Invalidate();
}
} void UserControl1_MouseHover(object sender, EventArgs e)
{
if (!this.error)
{
this.image = this.imageList1.Images[1];
this.Invalidate();
}
} void UserControl1_MouseDown(object sender, MouseEventArgs e)
{
if (!this.error)
{
this.image = this.imageList1.Images[2];
this.linGrBrush = new LinearGradientBrush(new Point(0, 0), new Point(0, 100), Color.Black, Color.Black);
this.Invalidate();
}
} }
}
设置:
e.Graphics.SmoothingMode=System.Drawing.Drawing2D.SmoothingMode.HighQuality 两个地方LZ 试试.
或者你的背景色影响了质量?