一、图像格式的转换
string strFilePathName = ImageShow.ImageUrl;
System.Drawing.Image i = System.Drawing.Image.FromFile(strFilePathName);
//以下得到在服务器上保存的文件路径名称
string strFileName = Path.GetFileNameWithoutExtension(strFilePathName);
ImageFormat f = ImageFormat.Bmp;
switch(ddlFormat.SelectedItem.Text.ToLower())//toLower为转换为小写
{
case "bmp":
break;
case "jpeg":
f = ImageFormat.Jpeg;
break;
case "gif":
f = ImageFormat.Gif;
break;
case "png":
f = ImageFormat.Png;
break;
case "tiff":
f = ImageFormat.Tiff;
break;
}
string strSeverPath=tbPath.Text+"\\"+strFileName+"."+
ddlFormat.SelectedItem.Text;
i.Save(strSeverPath,f);
System.Drawing.Image i = System.Drawing.Image.FromFile(strFilePathName);
//以下得到在服务器上保存的文件路径名称
string strFileName = Path.GetFileNameWithoutExtension(strFilePathName);
ImageFormat f = ImageFormat.Bmp;
switch(ddlFormat.SelectedItem.Text.ToLower())//toLower为转换为小写
{
case "bmp":
break;
case "jpeg":
f = ImageFormat.Jpeg;
break;
case "gif":
f = ImageFormat.Gif;
break;
case "png":
f = ImageFormat.Png;
break;
case "tiff":
f = ImageFormat.Tiff;
break;
}
string strSeverPath=tbPath.Text+"\\"+strFileName+"."+
ddlFormat.SelectedItem.Text;
i.Save(strSeverPath,f);
下面是实现在图片上写字、切割图片、拉伸和旋转
对图片的处理和显示最好放在不同的页面,负责回显示满的
比如写字,切割等按钮放在一个页面,对这些操作放在Session里面保存
比如按钮执行 Session["Add"] = true; Session["Path"] = InputFile.PostedFile.FileName是保存图片的路径,在改页面放一个图片控件比如:
genpicture.aspx这个是另一个页面,在这个页面来处理图像。
代码如下:
if(Session["Path"]!=null)
{
// Response.Clear();
// Response.Write("<br>");
// Response.Write("<br>");
System.Drawing.Image g=System.Drawing.Image.FromFile(Session["Path"].ToString());
System.Drawing.Imaging.ImageFormat f=g.RawFormat;
Bitmap b=new Bitmap(g);
if(Session["RotateFlip"]!=null)
b.RotateFlip(RotateFlipType.Rotate90FlipNone);//旋转图片
Graphics gh=Graphics.FromImage(b);
if(Session["Add"]!=null)
gh.DrawString("MSDN荣誉出品",new Font("宋体",50),Brushes.Red ,5,b.Height-100);//给图片上添加文字
// 拉伸图片
if(Session["Exp"]!=null)
{
gh.Clear(Color.White);
gh.DrawImage(g,new Rectangle(10,10,50,50),new Rectangle(0,0,g.Width,g.Height),GraphicsUnit.Pixel);//17
}
//"切割图片
if(Session["Cut"]!=null)
{
gh.Clear(Color.White);
gh.DrawImage(g,50,100,new Rectangle(80,80,410,410),GraphicsUnit.Pixel); //19
}
gh.SmoothingMode=SmoothingMode.AntiAlias;
b.Save(Response.OutputStream,f);
gh.Dispose();
b.Dispose();
g.Dispose();
Session["RotateFlip"]=null;
Session["Cut"]=null;
Session["Exp"]=null;
Session["Exp"]=null;
Session["Cut"]=null;
}
{
// Response.Clear();
// Response.Write("<br>");
// Response.Write("<br>");
System.Drawing.Image g=System.Drawing.Image.FromFile(Session["Path"].ToString());
System.Drawing.Imaging.ImageFormat f=g.RawFormat;
Bitmap b=new Bitmap(g);
if(Session["RotateFlip"]!=null)
b.RotateFlip(RotateFlipType.Rotate90FlipNone);//旋转图片
Graphics gh=Graphics.FromImage(b);
if(Session["Add"]!=null)
gh.DrawString("MSDN荣誉出品",new Font("宋体",50),Brushes.Red ,5,b.Height-100);//给图片上添加文字
// 拉伸图片
if(Session["Exp"]!=null)
{
gh.Clear(Color.White);
gh.DrawImage(g,new Rectangle(10,10,50,50),new Rectangle(0,0,g.Width,g.Height),GraphicsUnit.Pixel);//17
}
//"切割图片
if(Session["Cut"]!=null)
{
gh.Clear(Color.White);
gh.DrawImage(g,50,100,new Rectangle(80,80,410,410),GraphicsUnit.Pixel); //19
}
gh.SmoothingMode=SmoothingMode.AntiAlias;
b.Save(Response.OutputStream,f);
gh.Dispose();
b.Dispose();
g.Dispose();
Session["RotateFlip"]=null;
Session["Cut"]=null;
Session["Exp"]=null;
Session["Exp"]=null;
Session["Cut"]=null;
}
下面是对图片的一些基本操作画图
Bitmap b = new Bitmap(600,600);
Graphics g = Graphics.FromImage(b);//GDI+中最重要的类
g.Clear(Color.Red);
Pen p = new Pen(Color.Green,3.0f);//铅笔
g.DrawLine(p,0,0,600,600);
g.DrawLine(p,600,0,0,600);
g.DrawEllipse(p,0,0,100,100);
SolidBrush sb = new SolidBrush(Color.Blue);//固体刷
g.FillEllipse(sb,100,100,200,200);
g.FillRectangle(sb,300,300,100,100);
Font f = new Font("宋体",40);//字体
g.DrawString("哈哈,不错!",f,sb,0,300);
Point[] arrP = new Point[5];//point为基本的点
arrP[0] = new Point(200,200);
arrP[1] = new Point(200,400);
arrP[2] = new Point(500,400);
arrP[3] = new Point(500,600);
arrP[4] = new Point(300,600);
g.DrawPolygon(p,arrP);
b.Save(Response.OutputStream,ImageFormat.Gif);
Graphics g = Graphics.FromImage(b);//GDI+中最重要的类
g.Clear(Color.Red);
Pen p = new Pen(Color.Green,3.0f);//铅笔
g.DrawLine(p,0,0,600,600);
g.DrawLine(p,600,0,0,600);
g.DrawEllipse(p,0,0,100,100);
SolidBrush sb = new SolidBrush(Color.Blue);//固体刷
g.FillEllipse(sb,100,100,200,200);
g.FillRectangle(sb,300,300,100,100);
Font f = new Font("宋体",40);//字体
g.DrawString("哈哈,不错!",f,sb,0,300);
Point[] arrP = new Point[5];//point为基本的点
arrP[0] = new Point(200,200);
arrP[1] = new Point(200,400);
arrP[2] = new Point(500,400);
arrP[3] = new Point(500,600);
arrP[4] = new Point(300,600);
g.DrawPolygon(p,arrP);
b.Save(Response.OutputStream,ImageFormat.Gif);