本文介绍了删除PhotoUploader.cs到我的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,

i想要删除PhotoUploader.cs到我的代码:



Hello,
i want remove PhotoUploader.cs to this my code:

protected void RadButton39_Click(object sender, EventArgs e)
{

    try
    {

        HttpFileCollection uploadedFiles = Request.Files;

        PhotoUploader uploader = new PhotoUploader();



        for (int i = 0; i < uploadedFiles.Count; i++)
        {
            HttpPostedFile userPostedFile = uploadedFiles[i];

            if (userPostedFile.ContentLength > 0)
            {
                using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
                {

                    uploader.StartUploadProcess(userPostedFile, 210,  "~/Img/News/");
                    string small_pic_name = uploader.LastImageName;

                    uploader.StartUploadProcess(userPostedFile, 650, "~/Img/News/");
                    string big_pic_name = uploader.LastImageName;

                    string commandText = "INSERT INTO NewsGallery(ID,   Small_pic, Big_pic) VALUES ('" + int.Parse(GridView1.SelectedDataKey.Value.ToString()) + "','" + small_pic_name + "', '" + big_pic_name + "')";
                    SqlCommand command = new SqlCommand(commandText, connection);

                    connection.Open();
                    int rowsAffected = command.ExecuteNonQuery();
                    if (rowsAffected == 1)
                    {
                        // MessageLabel.Text = "";
                    }
                    connection.Close();

                }

            }

            GridView1.DataBind();
            GridView2.DataBind();


        }
    }
    catch { }
}





这是PhotoUploader.cs课程:





and this is PhotoUploader.cs Class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;


/// <summary>
/// Summary description for PhotoUploader
/// </summary>
public class PhotoUploader
{





    HttpPostedFile photo;
    string _imageName;
    string _path;
    string _outputMessage;
    int _newWidth;
    string _lastImageName;
    //GET AND SET IMAGE NAME
    public string ImageName
    {
        get { return _imageName; }
        set { _imageName = value; }
    }

    public string LastImageName
    {
        get { return _lastImageName; }
        set { _lastImageName = value; }
    }

    //GET AND SET IMAGE PATH
    public string Path
    {
        get { return _path; }
        set { _path = value; }
    }

    //GET AND SET NEW WIDTH
    public int NewWidth
    {
        get { return _newWidth; }
        set { _newWidth = value; }
    }

    //GET AND SET ERROR MESSAGE
    public string OutputMessage
    {
        get { return _outputMessage; }
        set { _outputMessage = value; }
    }

    public bool StartUploadProcess(HttpPostedFile ProductPhoto, int LargestWidthPX, string MapPath)
    {
        photo = ProductPhoto;
        ImageName = photo.FileName.Replace(" ", "").Replace("%20", "");

        //--- make sure image is in acceptable type
        if (ImageName.ToLower().EndsWith(".jpg") || ImageName.ToLower().EndsWith(".jpeg"))
        {
            if ((ImageName != null))
            {
                if (UploadPhoto(LargestWidthPX, MapPath))
                {
                    OutputMessage = "Photo was successfully uploaded.";
                    return true;
                }
            }
            else
            {
                OutputMessage = "No photo supplied. Please selecte a photo.";
                return false;
            }
        }
        else
        {
            OutputMessage = "The photo is not a supported type. Photo must be .jpg, .png or .gif";
            return false;
        }
        return false;
    }

    protected bool UploadPhoto(int LargestWidthPX, string MapPath)
    {
        Path = MapPath;
        photo.SaveAs(HttpContext.Current.Server.MapPath(Path + System.DateTime.Now.Day.ToString() + System.DateTime.Now.Month.ToString() + System.DateTime.Now.Year.ToString() + System.DateTime.Now.Hour.ToString() + System.DateTime.Now.Minute.ToString() + ImageName.Replace(" ", "").Replace("%20", "")));
        NewWidth = LargestWidthPX;

        if (resizeImage())
        {


            File.Delete(HttpContext.Current.Server.MapPath(Path + System.DateTime.Now.Day.ToString() + System.DateTime.Now.Month.ToString() + System.DateTime.Now.Year.ToString() + System.DateTime.Now.Hour.ToString() + System.DateTime.Now.Minute.ToString() + photo.FileName.Replace(" ", "").Replace("%20", "")));
            return true;


        }

        return false;
    }
    protected bool resizeImage()
    {
        string originalFileName = null;
        string newFileName = null;
        Bitmap tmpImage = default(Bitmap);
        Bitmap newImage = default(Bitmap);
        Graphics g = default(Graphics);
        int newHeight = 0;
        FileStream fs = default(FileStream);
        originalFileName = HttpContext.Current.Server.MapPath(Path + System.DateTime.Now.Day.ToString() + System.DateTime.Now.Month.ToString() + System.DateTime.Now.Year.ToString() + System.DateTime.Now.Hour.ToString() + System.DateTime.Now.Minute.ToString() + ImageName.Replace(" ", "").Replace("%20", ""));
        if (File.Exists(originalFileName))
        {
            try
            {
                fs = new FileStream(originalFileName, FileMode.Open);
                tmpImage = (Bitmap)Bitmap.FromStream(fs);
                fs.Close();

                newHeight = (NewWidth * tmpImage.Height) / tmpImage.Width;
                newImage = new Bitmap(NewWidth, newHeight);
                g = Graphics.FromImage(newImage); g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality; g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality; g.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceOver; g.DrawImage(tmpImage, 0, 0, NewWidth, newHeight);
                g.Dispose();

                newFileName = NewWidth.ToString() + "_" + ImageName.Replace(" ", "").Replace("%20", "");
                newImage.Save(HttpContext.Current.Server.MapPath(Path + System.DateTime.Now.Day.ToString() + System.DateTime.Now.Month.ToString() + System.DateTime.Now.Year.ToString() + System.DateTime.Now.Hour.ToString() + System.DateTime.Now.Minute.ToString() + newFileName), System.Drawing.Imaging.ImageFormat.Jpeg);

                LastImageName = "~/Img/News/" + System.DateTime.Now.Day.ToString() + System.DateTime.Now.Month.ToString() + System.DateTime.Now.Year.ToString() + System.DateTime.Now.Hour.ToString() + System.DateTime.Now.Minute.ToString() + newFileName;


                newImage.Dispose();
                tmpImage.Dispose();

                tmpImage = null;
                newImage = null;
                g = null;
                return true;
            }
            catch (Exception ex)
            {
                OutputMessage = "There was an error processing the request and we have been notified. Please try again later.";
                tmpImage = null;
                newImage = null;
                g = null;
                return false;
            }
        }
        else
        {
            OutputMessage = "There was an error processing the request and we have been notified. Please try again later.";
            tmpImage = null;
            newImage = null;
            g = null;
            return false;
        }
    }

}





请帮助我。



谢谢



Please Help me.

thanks

推荐答案

这篇关于删除PhotoUploader.cs到我的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 02:42