在列表视图asp点网内的图像错误上的水印

时间:2016-06-04 19:20:06

标签: asp.net watermark

我有这个c#asp dot网络代码在asp图像控件上添加水印,工作正常。

           string watermarkText = "© water mark";
            string fileName = Server.MapPath(myimg.ImageUrl);
            FileStream fs = new FileStream(fileName, FileMode.Open);
            using (Bitmap bmp = new Bitmap(fs, false))
            {
                using (Graphics grp = Graphics.FromImage(bmp))
                {
                    Brush brush = new SolidBrush(Color.Red);
                    Font font = new System.Drawing.Font("Arial", 30, FontStyle.Bold, GraphicsUnit.Pixel);
                    SizeF textSize = new SizeF();
                    textSize = grp.MeasureString(watermarkText, font);
                    Point position = new Point((bmp.Width - ((int)textSize.Width + 10)), (bmp.Height - ((int)textSize.Height + 80)));
                    grp.DrawString(watermarkText, font, brush, position);


                    using (MemoryStream memoryStream = new MemoryStream())
                    {

                        bmp.Save(memoryStream, ImageFormat.Png);
                        string base64String = Convert.ToBase64String(memoryStream.ToArray());
                        string imageUrl = "data:image/png;base64," + base64String;
                        myimg.Attributes.Add("src", imageUrl);


                    }
                }
            }

但是当我在listview数据绑定事件中的listview中添加相同的水印代码时,如

    System.Web.UI.WebControls.Image myimg;
protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e)
{

    if (!IsPostBack)
    {
        if (e.Item.ItemType == ListViewItemType.DataItem)
        {
            myimg = ((System.Web.UI.WebControls.Image)e.Item.FindControl("Image1"));



            string watermarkText = "© watermark";
            string fileName = Server.MapPath(myimg.ImageUrl);
            FileStream fs = new FileStream(fileName, FileMode.Open);
            using (Bitmap bmp = new Bitmap(fs, false))
            {
                using (Graphics grp = Graphics.FromImage(bmp))
                {
                    Brush brush = new SolidBrush(Color.Red);
                    Font font = new System.Drawing.Font("Arial", 30, FontStyle.Bold, GraphicsUnit.Pixel);
                    SizeF textSize = new SizeF();
                    textSize = grp.MeasureString(watermarkText, font);
                    Point position = new Point((bmp.Width - ((int)textSize.Width + 10)), (bmp.Height - ((int)textSize.Height + 80)));
                    grp.DrawString(watermarkText, font, brush, position);


                    using (MemoryStream memoryStream = new MemoryStream())
                    {

                        bmp.Save(memoryStream, ImageFormat.Png);
                        string base64String = Convert.ToBase64String(memoryStream.ToArray());
                        string imageUrl = "data:image/png;base64," + base64String;
                        myimg.Attributes.Add("src", imageUrl);


                    }
                }
            }
        }
    }
}

所以它给了我以下错误CustomCoupon \ ca00453f-c985-4794-9a87-36a60e2fa0e1.png'因为它正被另一个进程使用。

请建议。

1 个答案:

答案 0 :(得分:1)

您可以替换:

FileStream fs = new FileStream(fileName, FileMode.Open);

由:

using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read))
{
    ...
}