如何修复System.IO.FileNotFoundException

时间:2019-01-05 13:06:58

标签: c# asp.net

//front end coding 
 <div class="form-group">
    <asp:FileUpload ID = "fupPhoto" runat="server" BorderColor="#EFEFEF" ForeColor="#9A9A9A"/>
 </div>
//back end coding 
protected void btnInsert_Click(object sender, EventArgs e)
{
    if (Page.IsValid)
    {
        string filename = Path.GetFileNameWithoutExtension(fupPhoto.PostedFile.FileName) + ".jpg";
        System.Drawing.Image image = Bitmap.FromFile(filename);
        Graphics graphics = Graphics.FromImage(image);
        StringFormat stringFormat = new StringFormat();
        stringFormat.Alignment = StringAlignment.Center; 
        Color color = ColorTranslator.FromHtml("#D3D3D3");
        string text = "Culturely.Co"; 
        graphics.DrawString(text, new Font("Arail", 30, FontStyle.Bold), new SolidBrush(color), new Point(268, 245)); 
        image.Save("watermarked" + filename); 
    }
}

我希望在图像上添加水印并与页面保存在同一文件中,但是单击按钮后显示System.IO.FileNotFoundException错误。

1 个答案:

答案 0 :(得分:0)

        if (Page.IsValid)
        {
            System.Drawing.Image image = new System.Drawing.Bitmap(fupPhoto.PostedFile.InputStream); //Bitmap.FromFile(filename);
            Graphics graphics = Graphics.FromImage(image);
            StringFormat stringFormat = new StringFormat();
            stringFormat.Alignment = StringAlignment.Center;
            Color color = ColorTranslator.FromHtml("#D3D3D3");
            string text = "Culturely.Co";
            graphics.DrawString(text, new Font("Arail", 30, FontStyle.Bold), new SolidBrush(color), new Point(268, 245));
            //image.Save("watermarked" + filename);
            string filename = fupPhoto.FileName;
            string outputname = "Watermarked" + filename;
            image.Save("C:\\" + outputname);

无法替换文件。因此,需要使用另一个名称然后它才能运行。 感谢您的帮助。