asp.Netは画像をアップロードして透かしとサムネイルを処理するインスタンスコードをします

9398 ワード

メソッドクラス:
 
  
upFileClass.cs

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

///


/// upFileClass
///

public class upFileClass
{
    public upFileClass()
    {
        //
        // TODO:
        //
    }

    ///


    ///
    ///

    /// ( )
    /// ( )
    ///
    ///
    ///
    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();
        }
    }

    ///


    ///
    ///

    ///
    ///
    public static void AddShuiYinWord(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", 16);
        System.Drawing.Brush b = new System.Drawing.SolidBrush(System.Drawing.Color.Blue);

        g.DrawString(addText, f, b, 15, 15);
        g.Dispose();

        image.Save(Path_sy);
        image.Dispose();
    }

    ///


    ///
    ///

    ///
    ///
    ///
    public static void AddShuiYinPic(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();
    }
}
 


Default.aspxコード:
 
  



    asp.net


   

  

       
       

   



 

Default.aspx.csコード:
 
 
  
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (FileUpload1.HasFile)
        {
            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);
                string fileName = file.Name; //
                string fileName_s = "s_" + file.Name; //
                string fileName_sy = "sy_" + file.Name; // ( )
                string fileName_syp = "syp_" + file.Name; // ( )
                //
                string webFilePath = Server.MapPath("file/" + fileName); //
                string webFilePath_s = Server.MapPath("file/" + fileName_s);  //
                string webFilePath_sy = Server.MapPath("file/" + fileName_sy); // ( )
                string webFilePath_syp = Server.MapPath("file/" + fileName_syp); // ( )
                // !
                string webFilePath_sypf = Server.MapPath("file/shuiyin.jpg"); // ( )

                if (!File.Exists(webFilePath))
                {
                    try
                    {
                        FileUpload1.SaveAs(webFilePath); // SaveAs
                        upFileClass.AddShuiYinWord(webFilePath, webFilePath_sy);
                        upFileClass.AddShuiYinPic(webFilePath, webFilePath_syp, webFilePath_sypf);
                        upFileClass.MakeThumbnail(webFilePath, webFilePath_s, 130, 130, "Cut"); //
                        Label1.Text = " : “" + fileName + "” , “" + fileName_s + "” , :" + FileUpload1.PostedFile.ContentType + ", :" + FileUpload1.PostedFile.ContentLength + "B";
                    }
                    catch (Exception ex)
                    {
                        Label1.Text = " : , :" + ex.Message;
                    }
                }
                else
                {
                    Label1.Text = " : , ";
                }
            }
            else
            {
                Label1.Text = " : ";
            }

        }
    }

}