using System.IO;
using System.Drawing.Imaging;
using System.Text.RegularExpressions;//限制输入的引用
图片变圆
#region 图片变圆形的方法
private Image CutEllipse(Image img, Rectangle rec, Size size)
{
Bitmap bitmap = new Bitmap(size.Width, size.Height);
using (Graphics g = Graphics.FromImage(bitmap))
{
if (pictureBox1.Image!= null)
{
using (TextureBrush br = new TextureBrush(img, System.Drawing.Drawing2D.WrapMode.Clamp, rec))
{
br.ScaleTransform(bitmap.Width / (float)rec.Width, bitmap.Height / (float)rec.Height);
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
g.FillEllipse(br, new Rectangle(Point.Empty, size));
}
}
}
return bitmap;
}
#endregion
#region 修改图片(保存在数据库,调出数据库图片)
public void SaveImage(OpenFileDialog OpenF)//将文件转换成数据流
{
string strImage = OpenF.FileName;
FileStream fs = new FileStream(strImage, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
byte[] imageByte = br.ReadBytes((int)fs.Length);
user.photostr = imageByte;//可以将二进制更新在数据库里(全局变量user)
if (user.photostr == null)
{
lblModify.Text = "请修改头像";
}
else
{
bool updatePic= facade.UpdateUserPic(user);
}
StreamToFile(imageByte);
}
public void StreamToFile(byte[] imageByte)//将数据流转换成文件
{
if (imageByte != null)
{
//显示图片
MemoryStream ms = new MemoryStream(imageByte);
pictureBox1.Image = Image.FromStream(ms);
int width = pictureBox1.Image.Width;//这是宽
int height = pictureBox1.Image.Height;//这是高
//图片变成圆形
Image image = this.pictureBox1.Image;
Image newImage = CutEllipse(image, new Rectangle(0, 0, width, height), new Size(150, 150));
this.lblHead.Image = newImage;
}
}
private void lblHead_Click(object sender, EventArgs e)
{
string Photo;
bool isUpLoadPicture = false;
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "*.jpg|*.jpg|*.png|*.png|*.bmp|*.bmp";
if (ofd.ShowDialog() == DialogResult.OK)
{
isUpLoadPicture = true;
Photo = ofd.FileName;//实际文件的路径
pictureBox1.Image = Image.FromFile(Photo);
pictureBox1.ImageLocation = Photo;
SaveImage(ofd);
}
}
#endregion