C#批量上传图片
发布日期:2021-05-13 07:42:15 浏览次数:8 分类:博客文章

本文共 13750 字,大约阅读时间需要 45 分钟。

���������������������������

        ���1���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

        ���2���������������������������������������������������������������������������

        ���3������������������������������������������

        ���4���������������������������������������������������������������������������������������������

        ���5������������������������������������������������

        ������������������������������������������������������������������������������������

    ���������������

using System;

using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
using System.Net;
using System.Text.RegularExpressions;

/// ���summary>

/// FileUpload1.HasFile ���������true���������������������������������������
/// FileUpload1.FileName ������������������������������������������������������
/// FileUpload1.FileContent ������������������������������������������
/// FileUpload1.PostedFile   ���������������������������������
/// FileUpload1.PostedFile.ContentLength ���������������������������������������������������
/// FileUpload1.PostedFile.ContentType    ���������������������MIME������������������������������������������������"image/jpg"
/// FileUpload1.PostedFile.FileName       ������������������������������������������������������������������
/// FileUpload1.PostedFile.InputStream    ������������������������������������������
/// FileInfo���������������������������������������������������������������������������������������������FileInfo���������
/// ���/summary>

public partial class BackManagement_ImagesUpload : System.Web.UI.Page

{
    public string treePath = "";
    public int imageW = 100;
    public int imageH = 100;
    protected void Page_Load(object sender, EventArgs e)
    {
        this.Button5.Attributes.Add("Onclick", "window.close();"); //������������������������������������������������������������������������������
        if (!Page.IsPostBack)
        {
            Label2.Text = Server.MapPath("/");
            TextBox3.Text = "ImageUpload";
            treePath = Server.MapPath("/") + TextBox3.Text.Trim() + "/";
            TextBox4.Text = imageW.ToString();
            TextBox5.Text = imageH.ToString();
        }
    }
    protected void btnload_Click(object sender, EventArgs e)
    {
        //���������������������������������������������������
        treePath = Server.MapPath("/") + TextBox3.Text.Trim() + "/";
        imageW = Convert.ToInt32(TextBox4.Text.ToString());
        imageH = Convert.ToInt32(TextBox5.Text.ToString());
        if (!File.Exists(treePath + "images"))   //������/ImageUpload/images���������������������/ImageUpload/images������������������������
        {
            System.IO.Directory.CreateDirectory(treePath + "images");
        }
        if (!File.Exists(treePath + "thumbnails"))   //������/ImageUpload/thumbnails���������������������/ImageUpload/thumbnails���������������������������
        {
            System.IO.Directory.CreateDirectory(treePath + "thumbnails");
        }
        if (!File.Exists(treePath + "textImages"))   //������/ImageUpload/textImages���������������������/ImageUpload/textImages������������������������������
        {
            System.IO.Directory.CreateDirectory(treePath + "textImages");
        }
        if (!File.Exists(treePath + "waterImages"))   //������/ImageUpload/waterImages���������������������/ImageUpload/waterImages������������������������������
        {
            System.IO.Directory.CreateDirectory(treePath + "waterImages");
        }

        if (FileUpload1.HasFile)   //���������true���������������������������������������

        {
            string fileContentType = FileUpload1.PostedFile.ContentType;
            if (fileContentType == "image/bmp" || fileContentType == "image/gif" || fileContentType == "image/pjpeg")
            {
                string name = FileUpload1.PostedFile.FileName;                         //������������������������������������������������������������������

                FileInfo file = new FileInfo(name);                                    //FileInfo���������������������������������������������������������������������������������������������FileInfo���������

                string fileName = file.Name;                                           // ������������
                string fileName_s = "x_" + file.Name;                                  // ���������������������
                string fileName_sy = "text_" + file.Name;                              // ���������������������������������
                string fileName_syp = "water_" + file.Name;                            // ���������������������������������

                string webFilePath = treePath + "images/" + fileName;          // ������������������������

                string webFilePath_s = treePath + "thumbnails/" + fileName_s; // ���������������������������
                string webFilePath_sy = treePath + "textImages/" + fileName_sy; // ������������������������������(������)
                string webFilePath_syp = treePath + "waterImages/" + fileName_syp; // ������������������������������(������)
                string webFilePath_sypf = Server.MapPath("../images/tzwhx.png");               // ���������������������������(������)
                if (!File.Exists(webFilePath))
                {
                    try
                    {
                        FileUpload1.SaveAs(webFilePath);                                // ������ SaveAs ������������������
                        if (CheckBox1.Checked)                                          //���������������������������
                        {
                            AddWater(webFilePath, webFilePath_sy);
                        }
                        if (CheckBox2.Checked)                                          //���������������������������
                        {
                            AddWaterPic(webFilePath, webFilePath_syp, webFilePath_sypf);
                        }
                        MakeThumbnail(webFilePath, webFilePath_s, imageW, imageH, "Cut");     // ���������������������
                        Label1.Text = "������������������" + fileName + "������������������������������" + fileName_s + "���������������������������������" + FileUpload1.PostedFile.ContentType + "���������������������" + FileUpload1.PostedFile.ContentLength + "B";
                        Image1.ImageUrl = "/" + TextBox3.Text.ToString() + "/images/" + fileName;
                        TextBox1.Text = webFilePath;
                        TextBox2.Text = "/" + TextBox3.Text.ToString() + "/images/" + fileName;
                    }
                    catch (Exception ex)
                    {
                        Label1.Text = "���������������������������������������������" + ex.Message;
                    }
                }
                else
                {
                    Label1.Text = "���������������������������������������������������";
                }
            }
            else
            {
                Label1.Text = "���������������������������";
            }
        }
    }
    /**/
    /// ���summary>
    /// ���������������
    /// ���/summary>
    /// ���param name="originalImagePath">���������������������������������/param>
    /// ���param name="thumbnailPath">������������������������������������/param>
    /// ���param name="width">������������������/param>
    /// ���param name="height">������������������/param>
    /// ���param name="mode">���������������������������/param>  
    public static void MakeThumbnail(string originalImagePath, string thumbnailPath, int width, int height, string mode)
    {
        System.Drawing.Image originalImage = System.Drawing.Image.FromFile(originalImagePath);

        int towidth = width;

        int toheight = height;

        int x = 0;

        int y = 0;
        int ow = originalImage.Width;
        int oh = originalImage.Height;

        switch (mode)

        {
            case "HW"://������������������������������������           
                break;
            case "W"://������������������������                
                toheight = originalImage.Height * width / originalImage.Width;
                break;
            case "H"://������������������������
                towidth = originalImage.Width * height / originalImage.Height;
                break;
            case "Cut"://���������������������������������           
                if ((double)originalImage.Width / (double)originalImage.Height > (double)towidth / (double)toheight)
                {
                    oh = originalImage.Height;
                    ow = originalImage.Height * towidth / toheight;
                    y = 0;
                    x = (originalImage.Width - ow) / 2;
                }
                else
                {
                    ow = originalImage.Width;
                    oh = originalImage.Width * height / towidth;
                    x = 0;
                    y = (originalImage.Height - oh) / 2;
                }
                break;
            default:
                break;
        }

        //������������bmp������

        System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight);

        //������������������

        System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);

        //������������������������

        g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;

        //���������������,���������������������������

        g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;

        //���������������������������������������

        g.Clear(System.Drawing.Color.Transparent);

        //������������������������������������������������������������������

        g.DrawImage(originalImage, new System.Drawing.Rectangle(0, 0, towidth, toheight),
            new System.Drawing.Rectangle(x, y, ow, oh),
            System.Drawing.GraphicsUnit.Pixel);

        try

        {
            //���jpg���������������������
            bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg);
        }
        catch (System.Exception e)
        {
            throw e;
        }
        finally
        {
            originalImage.Dispose();
            bitmap.Dispose();
            g.Dispose();
        }
    }

    /**/

    /// ���summary>
    /// ������������������������������
    /// ���/summary>
    /// ���param name="Path">���������������������������/param>
    /// ���param name="Path_sy">������������������������������������������/param>
    protected void AddWater(string Path, string Path_sy)
    {
        string addText = "";
        System.Drawing.Image image = System.Drawing.Image.FromFile(Path);
        System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(image);
        g.DrawImage(image, 0, 0, image.Width, image.Height);
        System.Drawing.Font f = new System.Drawing.Font("Verdana", 10);    //���������������������10
        System.Drawing.Brush b = new System.Drawing.SolidBrush(System.Drawing.Color.Green);

        g.DrawString(addText, f, b, 14, 14);    //���������������14X14

        g.Dispose();

        image.Save(Path_sy);

        image.Dispose();
    }

    /**/

    /// ���summary>
    /// ������������������������������
    /// ���/summary>
    /// ���param name="Path">���������������������������/param>
    /// ���param name="Path_syp">������������������������������������������/param>
    /// ���param name="Path_sypf">���������������������/param>
    protected void AddWaterPic(string Path, string Path_syp, string Path_sypf)
    {
        System.Drawing.Image image = System.Drawing.Image.FromFile(Path);
        System.Drawing.Image copyImage = System.Drawing.Image.FromFile(Path_sypf);
        System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(image);
        g.DrawImage(copyImage, new System.Drawing.Rectangle(image.Width - copyImage.Width, image.Height - copyImage.Height, copyImage.Width, copyImage.Height), 0, 0, copyImage.Width, copyImage.Height, System.Drawing.GraphicsUnit.Pixel);
        g.Dispose();

        image.Save(Path_syp);

        image.Dispose();
    }

    protected void Button2_Click(object sender, EventArgs e)

    {
        //������������������������
        WebClient client = new WebClient();
        //������Reg:���img.*?src=([\"\'])(http:\/\/.+\.(jpg|gif|bmp|bnp))\1.*?>
        Regex reg = new Regex("IMG[^>]*?src\\s*=\\s*(?:\"(?���1>[^\"]*)\"|'(?���1>[^\']*)')", RegexOptions.IgnoreCase);
        MatchCollection m = reg.Matches(TextBox1.Text);

        foreach (Match math in m)

        {
            string imgUrl = math.Groups[1].Value;

            //������������������������YYMMDD������������������

            Regex regName = new Regex(@"\w+.(?:jpg|gif|bmp|png)", RegexOptions.IgnoreCase);

            string strNewImgName = DateTime.Now.ToShortDateString().Replace("-", "") + regName.Match(imgUrl).ToString();

            try

            {
                //������������
                //client.DownloadFile(imgUrl, Server.MapPath("../ImageUpload/Auto/" + strNewImgName));
            }
            catch
            {
            }
            finally
            {

            }

            client.Dispose();

        }

        Response.Write("���script>alert('������������������������������������������ImageUpload/auto')���/script>");

    }
}

     ���������������

���%@ Page Language="C#" AutoEventWireup="true" CodeFile="ImagesAutoUpload.aspx.cs" Inherits="BackManagement_ImagesAutoUpload" %>

���!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">

���html xmlns="">

���head id="Head1" runat="server">
    ���title>���������������������������������������/title>
���/head>
���body>
    ���form id="form1" runat="server" method="post" enctype="multipart/form-data">
    ���label for="pagebody1" style="display: none">
    ���/label>
    ���fieldset id="container">
        ���legend>���������������������������������/legend>
        ���div class="window" style="list-style: none;">
            ���div style="padding: 0px; width: 808px; height: 200px; float: left; margin-right: 0px;">
                ���ul>
                    ���li style="width: 150px; margin: 0px; padding: 0px; float: left;">
                        ���asp:Image ID="Image1" runat="server" Height="200px" BorderWidth="2px" Width="150px"
                            ImageUrl="~/images/Jpg/135X67/0_11_16.gif" />
                    ���/li>
                    ���li style="width: 250px; margin: 0px;">
                        ���asp:ListBox ID="FileList" runat="server" Width="250px" Height="200px">���/asp:ListBox>
                    ���/li>
                    ���li style="width: 400px; margin: 0px; float: right;">
                        (1)���������������������������������������asp:TextBox ID="TextBox3" runat="server">���/asp:TextBox>���������������������������������������������������������������br />
                        (2)��������������������������������������������� /images������������������ /thumbnails������������������������ /textImages������������������������ /waterImages���������������br />
                        (3)������������������������������������������������100px��������������������������������������� ���asp:TextBox ID="TextBox4" runat="server" Width="54px">���/asp:TextBox>������������asp:TextBox
                            ID="TextBox5" runat="server" Width="56px">���/asp:TextBox>���br />
                        ���asp:CheckBox ID="CheckBox1" runat="server" Text="������������" />
                        ���asp:CheckBox ID="CheckBox2" runat="server" Text="������������" />
                            ��������������������������������������������������������� ���/li>
                ���/ul>
            ���/div>
        ���/div>
        ���div>
            ���asp:FileUpload ID="FindFile" runat="server" Width="529px" />
        ���/div>
        ���div>
            ���asp:TextBox ID="TipInfo" runat="server" Width="400px">���/asp:TextBox>
            ���asp:TextBox ID="TextBox1" runat="server" Width="400px">���/asp:TextBox>
        ���/div>
        ���div>
            ���asp:Button ID="Upload" runat="server" Text="��� ���" Style="height: 26px" OnClick="Upload_Click" />
            ���asp:Button ID="AddFile" runat="server" Text="��� ���" OnClick="AddFile_Click1" />
            ���asp:Button ID="AddAllFile" runat="server" Text="������������" OnClick="AddAllFile_Click"
                Enabled="False" />
            ���asp:Button ID="DelFile" runat="server" Text="��� ���" OnClick="DelFile_Click1" />
            ���asp:Button ID="btnExit" runat="server" Text="��� ���" />
        ���/div>
        ���div>
        ���/div>
    ���/fieldset>
    ���/form>
���/body>
���/html>

        ���������������������������������������������������������Web.config������������������������

< system.web>

    < httpRuntime executionTimeout="90" maxRequestLength="20000" useFullyQualifiedRedirectUrl="false" requestLengthDiskThreshold="8192"/>

< /system.web>

 

���������

 

���������������������

上一篇:C# DataSet转IList代码
下一篇:jquery radio取值,checkbox取值,select取值及选中

发表评论

最新留言

不错!
[***.144.177.141]2025年05月04日 13时20分02秒