我有一张图片,想在图片的固定位置插一些文字,有没有实现的例子

解决方案 »

  1.   

    有啊,这就是传说中的图片上传,生成缩略图并加上水印的问题。也就是你的版权了
    网上有很多这样现成的控件,你也可以用代码:
    using System;
    using System.Drawing;
    using System.Drawing.Imaging;
    using System.Drawing.Drawing2D;
    using System.IO;
    namespace swfuploadtest
    {
    /// <summary>
    /// Summary description for WaterMark2.
    /// </summary>
         /* 
          *示例
            string wmFilePath = Server.MapPath(".") + "\\water.gif";
                string srcFilePath = Server.MapPath(".") + "\\aug.jpg";
                string srcnewpath = Server.MapPath(".") + "\\augnew.jpg";
                using (System.Drawing.Image wmImg = System.Drawing.Image.FromFile(wmFilePath))
                {
                    System.Drawing.Image srcImg = System.Drawing.Image.FromFile(srcFilePath);
                    WaterMark wm = new WaterMark(srcImg, wmImg);                wm.Alignment = ContentAlignment.TopLeft;
                    using (Graphics graphics = wm.GetWaterMarkImage())
                    {
                        graphics.DrawImage(srcImg, srcImg.Width, srcImg.Height);                    Bitmap bitmap1 = new Bitmap(srcImg);                    //保存图片到指定路径
                        //bitmap1.Save(srcnewpath, ImageFormat.Bmp);                    //图片输出出来
                        MemoryStream stream1 = new MemoryStream();
                        bitmap1.Save(stream1, ImageFormat.Bmp);
                        Response.ClearContent();
                        Response.ContentType = "image/Bmp";
                        Response.BinaryWrite(stream1.ToArray());
                    }
                }
         */
    public class WaterMark
    {
       private Image m_SrcImg;
       private Image m_WMImg;
       private ContentAlignment m_ImagePosition;   #region 公有属性   public ContentAlignment Alignment
       {
        set
        {
         m_ImagePosition = value;
        }
       }   #endregion   #region 构造函数   public WaterMark(Image srcImg, Image wmImg)
       {
        m_SrcImg    = srcImg;
        m_WMImg     = wmImg;    m_ImagePosition   = ContentAlignment.BottomRight;
       }   #endregion   #region 公有方法   public Graphics GetWaterMarkImage()
       {
        Graphics outGraphics = Graphics.FromImage(m_SrcImg);
       
        int phWidth = m_SrcImg.Width;
        int phHeight = m_SrcImg.Height;    //To achieve a transulcent water we will apply (2) color 
        //manipulations by defineing a ImageAttributes object and 
        //seting (2) of its properties.
        ImageAttributes imageAttributes = new ImageAttributes();    //The first step in manipulating the water image is to replace 
        //the background color with one that is trasparent (Alpha=0, R=0, G=0, B=0)
        //to do this we will use a Colormap and use this to define a RemapTable
        ColorMap colorMap = new ColorMap();    //My water was defined with a background of 100% Green this will
        //be the color we search for and replace with transparency
        colorMap.OldColor = Color.FromArgb(255, 0, 255, 0);
        colorMap.NewColor = Color.FromArgb(0, 0, 0, 0);    ColorMap[] remapTable = {colorMap};    imageAttributes.SetRemapTable(remapTable, ColorAdjustType.Bitmap);    //The second color manipulation is used to change the opacity of the 
        //water. This is done by applying a 5x5 matrix that contains the 
        //coordinates for the RGBA space. By setting the 3rd row and 3rd column 
        //to 0.3f we achive a level of opacity
        float[][] colorMatrixElements = { 
                 new float[] {1.0f, 0.0f, 0.0f, 0.0f, 0.0f},       
                 new float[] {0.0f, 1.0f, 0.0f, 0.0f, 0.0f},        
                 new float[] {0.0f, 0.0f, 1.0f, 0.0f, 0.0f},        
                 new float[] {0.0f, 0.0f, 0.0f, 0.9f, 0.0f},        
                 new float[] {0.0f, 0.0f, 0.0f, 0.0f, 1.0f}}; 
        ColorMatrix wmColorMatrix = new ColorMatrix(colorMatrixElements);    imageAttributes.SetColorMatrix(wmColorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);    //create a image object containing the water
        if(m_WMImg != null)
        {
         int wmWidth = m_WMImg.Width;
         int wmHeight = m_WMImg.Height;     //For this example we will place the water in the upper right
         //hand corner of the photograph. offset down 10 pixels and to the 
         //left 10 pixles     int xPosOfWm = 0;
         int yPosOfWm = 0;     switch(m_ImagePosition)
         {
          case ContentAlignment.BottomCenter: 
           xPosOfWm = (phWidth/2) - (wmWidth/2);
           yPosOfWm = (phHeight - wmHeight) - 10;
           break;      case ContentAlignment.BottomLeft:
           xPosOfWm = 10;
           yPosOfWm = (phHeight - wmHeight) - 10;
           break;      case ContentAlignment.BottomRight:
           xPosOfWm = ((phWidth - wmWidth)-10);
           yPosOfWm = (phHeight - wmHeight) - 10;
           break;      case ContentAlignment.MiddleCenter:
           xPosOfWm = (phWidth/2) - (wmWidth/2);
           yPosOfWm = (phHeight/2) - (wmHeight/2);
           break;      case ContentAlignment.MiddleLeft:
           xPosOfWm = 10;
           yPosOfWm = (phHeight/2) - (wmHeight/2);
           break;      case ContentAlignment.MiddleRight:
           xPosOfWm = ((phWidth - wmWidth)-10);
           yPosOfWm = (phHeight/2) - (wmHeight/2);
           break;      case ContentAlignment.TopCenter:
           xPosOfWm = (phWidth/2) - (wmWidth/2);
           yPosOfWm = 10;
           break;      case ContentAlignment.TopLeft:
           xPosOfWm = 10;
           yPosOfWm = 10;
           break;      case ContentAlignment.TopRight:
           //For this example we will place the water in the upper right
           //hand corner of the photograph. offset down 10 pixels and to the 
           //left 10 pixles       xPosOfWm = ((phWidth - wmWidth)-10);
           yPosOfWm = 10;
           break;     }     outGraphics.DrawImage(m_WMImg, 
          new Rectangle(xPosOfWm,yPosOfWm,wmWidth,wmHeight), //Set the detination Position
          0,                  // x-coordinate of the portion of the source image to draw. 
          0,                  // y-coordinate of the portion of the source image to draw. 
          wmWidth,            // Water Width
          wmHeight,      // Water Height
          GraphicsUnit.Pixel, // Unit of measurment
          imageAttributes);   //ImageAttributes Object
        }    return outGraphics;
       }   #endregion 
      

  2.   

    http://www.skycn.com/soft/17477.html  这个是做的控件
      

  3.   

    就是在图片上打上水印,网上有很多,建议你看看csdn 上青青月儿 的博客,上面有很多!