簡體   English   中英

使用內存流上傳圖像 asp.net c#

[英]Upload image with Memory stream asp.net c#

來自 aspsnippets.com 的教程: http : //www.aspsnippets.com/Articles/Create-Add-Watermark-Text-to-Images-Photo-in-ASPNet-using-C-and-VBNet.aspx

包括代碼:

protected void Upload(object sender, EventArgs e)
{
string watermarkText = "© ASPSnippets.com";

//Get the file name.
string fileName = Path.GetFileNameWithoutExtension(FileUpload1.PostedFile.FileName) + ".png";

//Read the File into a Bitmap.
using (Bitmap bmp = new Bitmap(FileUpload1.PostedFile.InputStream, false))
{
    using (Graphics grp = Graphics.FromImage(bmp))
    {
        //Set the Color of the Watermark text.
        Brush brush = new SolidBrush(Color.Red);

        //Set the Font and its size.
        Font font = new System.Drawing.Font("Arial", 30, FontStyle.Bold, GraphicsUnit.Pixel);

        //Determine the size of the Watermark text.
        SizeF textSize = new SizeF();
        textSize = grp.MeasureString(watermarkText, font);

        //Position the text and draw it on the image.
        Point position = new Point((bmp.Width - ((int)textSize.Width + 10)), (bmp.Height - ((int)textSize.Height + 10)));
        grp.DrawString(watermarkText, font, brush, position);

        using (MemoryStream memoryStream = new MemoryStream())
        {
            //Save the Watermarked image to the MemoryStream.
            bmp.Save(memoryStream, ImageFormat.Png);
            memoryStream.Position = 0;

            //Start file download.
            Response.Clear();
            Response.ContentType = "image/png";
            Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName);

            //Write the MemoryStream to the Response.
            memoryStream.WriteTo(Response.OutputStream);
            Response.Flush();
            Response.Close();
            Response.End();
        }
    }
}
}

它幫助我們下載帶水印的文件,但我想上傳到項目網絡中的文件夾中。 抱歉,這是我第一次使用內存流,所以我不知道如何上傳。 我想我必須改變:

bmp.Save(memoryStream, ImageFormat.Png);
            memoryStream.Position = 0;

            //Start file download.
            Response.Clear();
            Response.ContentType = "image/png";
            Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName);

            //Write the MemoryStream to the Response.
            memoryStream.WriteTo(Response.OutputStream);
            Response.Flush();
            Response.Close();
            Response.End();

但是我不知道怎么做,請幫我。 謝謝 !!!

            protected void UploadFile_Click(object sender, EventArgs e)
            {
         using (MemoryStream memStream = new MemoryStream(UploadFile.FileBytes))
        {
             using (FileStream fstream = new FileStream(@"C:/UploadFolder/" + 
                    UploadFile.FileName, FileMode.Create))
                {
                    memStream .WriteTo(fstream);
                }
            }
           }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM