c#通过Graphics处理图片以及画图(初步学习1)

 
using  System;
using  System.Data;
using  System.Configuration;
using  System.Web;
using  System.Web.Security;
using  System.Web.UI;
using  System.Web.UI.WebControls;
using  System.Web.UI.WebControls.WebParts;
using  System.Web.UI.HtmlControls;
using  System.Drawing;
using  System.Drawing.Imaging;

/// 
/// MakePicture 的摘要说明
/// 

public   class  MakePicture
{
    
string imgpath;
    
public string Imgpath get return imgpath; } set { imgpath = value; } }
    
string imgstyle;
    
public string Imgstyle get return imgstyle; } set { imgstyle = value; } }
    
string imgname;
    
public string Imgname get return imgname; } set { imgname = value; } }
    Brush brushBg 
= new SolidBrush(Color.White);
 
    
public MakePicture()
    
{
        
//
        
// TODO: 在此处添加构造函数逻辑
        
//
    }

    
public bool MakeImage()
    
{
        
if (imgname!=""&&Imgstyle!="")
        
{

            
int name_len = imgname.Length;
            
string path = imgpath + "/images/media/img/logo(" + Imgstyle.ToString() + ").gif";
            
string path_new = imgpath + "/images/media/img/" + Imgstyle.ToString() + "/"+imgname+".gif";
            
if (int.Parse(Imgstyle) == 1)
            
{
                Font font1 
= new Font("汉鼎简特粗黑"23, FontStyle.Regular);
                
                
                Brush brush1 
= new SolidBrush(Color.FromArgb(225,6,1));
                
int width = name_len * 35+114;
                Bitmap imgTemp 
= new Bitmap(width, 60);
                System.Drawing.Image image1 
= System.Drawing.Image.FromFile(path);
                Graphics gh 
= Graphics.FromImage(imgTemp);
                gh.FillRectangle(brushBg, 
00, width, 60);
                gh.TextRenderingHint
=System.Drawing.Text.TextRenderingHint.AntiAliasGridFit;
                gh.DrawImage(image1, 
00);
                gh.DrawString(imgname, font1, brush1, 
108,26);
                imgTemp.Save(path_new);
                font1.Dispose();
                brush1.Dispose();
                gh.Dispose();
                
return true;
            }

            
else if (int.Parse(Imgstyle) == 2)
            
{
                Font font2 
= new Font("方正小篆体"30); 
                
int width = name_len * 36;
                Bitmap imgTemp 
= new Bitmap(width, 46);//创建Image对象,大小为200 * 200
                Graphics g2 = Graphics.FromImage(imgTemp);//在Image对象上创建Graphics对象g2
                g2.FillRectangle(brushBg, 00, width, 46);
                g2.TextRenderingHint 
= System.Drawing.Text.TextRenderingHint.AntiAliasGridFit;
                
//g2.FillRectangle(Brushes.White, 0, 0, width, 42);//在Image图片上画一个填充矩形
                Color[] colors ={ Color.Red, Color.FromArgb(24,240,120), Color.Blue, Color.FromArgb(24240120) };
                SolidBrush brush 
= new SolidBrush(Color.Black);
                
for (int i = 0; i < name_len; i++)
                
{
                    
int  j = i%4;
                    brush.Color 
= colors[j];
                    g2.DrawString(imgname[i].ToString(), font2, brush, i 
* 302);
                }

                imgTemp.Save(path_new);
//将Image图片保存到本地
                font2.Dispose();
                brush.Dispose();
                g2.Dispose();
//销毁g1对象
                return true;
            }

            
else if(int.Parse(Imgstyle) == 3){
                Font font3 
= new Font("方正舒体"20);
                
int width=name_len*46;
                Bitmap imgTemp 
= new Bitmap(width, 42);
                Graphics g2 
= Graphics.FromImage(imgTemp);//在Image对象上创建Graphics对象g2
                g2.FillRectangle(brushBg, 00, width, 42);
                g2.TextRenderingHint 
= System.Drawing.Text.TextRenderingHint.AntiAliasGridFit;
                System.Drawing.Image image1 
= System.Drawing.Image.FromFile(path,false);
                SolidBrush brush 
= new SolidBrush(Color.Black);
                Color[] colors 
={ Color.FromArgb(60,240,120), Color.Yellow, Color.FromArgb(120,240,162), Color.Black };

                
for (int i = 0; i < name_len; i++)
                
{
                    
int font_width=i*45+4;
                    
int pic_width=i*45;
                    
int j = i % 4;
                    brush.Color 
= colors[j];
                    g2.DrawImage(image1, pic_width, 
0);
                    g2.DrawString(imgname[i].ToString(), font3, brush, font_width, 
2);
                    
                }

                
                imgTemp.Save(path_new);
//将Image图片保存到本地
                font3.Dispose();
                brush.Dispose();
                g2.Dispose();
//销毁g1对象
                return true;
            }

            
else return false; }
        }

        
else
        
{
            
return false;
        }

        brushBg.Dispose();
    }

}

你可能感兴趣的:(web开发)