[软件设计/软件工程] .NET 图片上传

[复制链接]
发表于 2022-5-2 13:19:14
  /// <summary>
    /// asp.net上传图片并生成缩略图
    /// </summary>
    /// <param name="upImage">HtmlInputFile控件</param>
    /// <param name="sSavePath">保存的路径,些为相对服务器路径的下的文件夹</param>
    /// <param name="sThumbExtension">缩略图的thumb</param>
    /// <param name="intThumbWidth">生成缩略图的宽度</param>
    /// <param name="intThumbHeight">生成缩略图的高度</param>
    /// <returns>缩略图名称</returns>
    public string UpLoadImage(FileUpload upImage, string savePath, string sThumbExtension, int intThumbWidth, int intThumbHeight)
    {
        if (upImage.HasFile)
        {
            if (upImage.PostedFile.ContentLength > 0 && upImage.PostedFile.ContentLength / 1024 < 1024)
            {
                string imgType = upImage.PostedFile.ContentType.ToString(); // 图片类型
                if (imgType.Equals("image/pjpeg") || imgType.Equals("image/gif") || imgType.Equals("image/png"))
                {
                    //取得上传文件的扩展名
                    string strExt = upImage.PostedFile.FileName.Substring(upImage.PostedFile.FileName.LastIndexOf("."));
                    // 自定义名称保存
                    string newImaName = (sThumbExtension + strExt).ToString();
                    // 保存到指定目录
                    string newsavePath = Server.MapPath(savePath);

                    System.Drawing.Image img = System.Drawing.Image.FromFile(upImage.PostedFile.FileName);
                    if (img.Width < intThumbWidth && img.Height < intThumbHeight)
                    {
                        return "图片的宽度和高度不符合(" + intThumbWidth + "*" + intThumbHeight+")";
                    }
                    else
                    {
                        upImage.PostedFile.SaveAs(newsavePath + newImaName);
                        return "上传图片成功.";
                    }

                  
                    //try
                    //{
                    //    //原图加载
                    //    using (System.Drawing.Image sourceImage = System.Drawing.Image.FromFile(upImage.PostedFile.FileName))
                    //    {
                    //        //原图宽度和高度
                    //        int width = sourceImage.Width;
                    //        int height = sourceImage.Height;
                    //        int smallWidth;
                    //        int smallHeight;
                    //        //获取第一张绘制图的大小,(比较 原图的宽/缩略图的宽  和 原图的高/缩略图的高)
                    //        if (((decimal)width) / height <= ((decimal)intThumbWidth) / intThumbHeight)
                    //        {
                    //            smallWidth = intThumbWidth;
                    //            smallHeight = intThumbWidth * height / width;
                    //        }
                    //        else
                    //        {
                    //            smallWidth = intThumbHeight * width / height;
                    //            smallHeight = intThumbHeight;
                    //        }

                    //        // 新建一个图板,以最小等比例压缩大小绘制原图
                    //        using (System.Drawing.Image bitmap = new System.Drawing.Bitmap(smallWidth, smallHeight))
                    //        {
                    //            //绘制中间图
                    //            using (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(Color.Black);
                    //                g.DrawImage(
                    //                    sourceImage,
                    //                    new System.Drawing.Rectangle(0, 0, smallWidth, smallHeight),
                    //                    new System.Drawing.Rectangle(0, 0, width, height),
                    //                    System.Drawing.GraphicsUnit.Pixel);
                    //            }
                    //            //新建一个图板,以缩略图大小绘制中间图
                    //            using (System.Drawing.Image bitmap1 = new System.Drawing.Bitmap(intThumbWidth, intThumbHeight))
                    //            {
                    //                //绘制缩略图
                    //                using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap1))
                    //                {
                    //                    //高清,平滑
                    //                    g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
                    //                    g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                    //                    g.Clear(Color.Black);
                    //                    int lwidth = (smallWidth - intThumbWidth) / 2;
                    //                    int bheight = (smallHeight - intThumbHeight) / 2;
                    //                    g.DrawImage(bitmap, new Rectangle(0, 0, intThumbWidth, intThumbHeight), lwidth, bheight, intThumbWidth, intThumbHeight, GraphicsUnit.Pixel);
                    //                    g.Dispose();
                    //                    //缩略图保存的绝对路径
                    //                    bitmap1.Save(newsavePath, System.Drawing.Imaging.ImageFormat.Jpeg);
                    //                }
                    //            }
                    //        }

                    //    }
                    //}
                    //catch(Exception ex)
                    //{
                    //   return ex.Message.ToString();
                    //}
                  
                }
                else
                {
                    return "图片类型不正确.";
                }

            }
            else
            {
                return "请选择不大于1M的图片.";
            }
        }
        else
        {
            return "没有选择图片";
        }
    }

(/// <summary>
/// asp. Net upload pictures and generate thumbnails
/// </summary>
///< param name = "upimage" > htmlinputfile control < / param >
///< param name = "ssavepath" > the saved paths are the folders under the relative server path < / param >
///< param name = "sthumbextension" > thumb of thumbnail < / param >
///< param name = "intthumbwidth" > width of generated thumbnail < / param >
///< param name = "intthumbheight" > height of generated thumbnail < / param >
///< returns > thumbnail name < / returns >
public string UpLoadImage(FileUpload upImage, string savePath, string sThumbExtension, int intThumbWidth, int intThumbHeight)
{
if (upImage.HasFile)
{
if (upImage.PostedFile.ContentLength > 0 && upImage.PostedFile.ContentLength / 1024 < 1024)
{
string imgType = upImage. PostedFile. ContentType. ToString(); //  Picture type
if (imgType.Equals("image/pjpeg") || imgType. Equals("image/gif") || imgType. Equals("image/png"))
{
//Gets the extension of the uploaded file
string strExt = upImage. PostedFile. FileName. Substring(upImage.PostedFile.FileName.LastIndexOf("."));
//Custom name save
string newImaName = (sThumbExtension + strExt). ToString();
//Save to specified directory
string newsavePath = Server. MapPath(savePath);
System. Drawing. Image img = System. Drawing. Image. FromFile(upImage.PostedFile.FileName);
if (img.Width < intThumbWidth && img.Height < intThumbHeight)
{
Return "the width and height of the picture do not match (" + intthumbwidth + "*" + intthumbheight + ")";
}
else
{
upImage. PostedFile. SaveAs(newsavePath + newImaName);
Return "the picture was uploaded successfully.";
}
//try
//{
/// / original image loading
//    using (System.Drawing.Image sourceImage = System.Drawing.Image.FromFile(upImage.PostedFile.FileName))
//    {
/// / original width and height
//        int width = sourceImage. Width;
//        int height = sourceImage. Height;
//        int smallWidth;
//        int smallHeight;
/// / obtain the size of the first drawing (compare the width of the original drawing / the width of the thumbnail with the height of the original drawing / the height of the thumbnail)
//        if (((decimal)width) / height <= ((decimal)intThumbWidth) / intThumbHeight)
//        {
//            smallWidth = intThumbWidth;
//            smallHeight = intThumbWidth * height / width;
//        }
//        else
//        {
//            smallWidth = intThumbHeight * width / height;
//            smallHeight = intThumbHeight;
//        }
/// / create a new drawing board and draw the original drawing with the minimum equal scale compression size
//        using (System.Drawing.Image bitmap = new System.Drawing.Bitmap(smallWidth, smallHeight))
//        {
/// / draw the middle diagram
//            using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap))
//            {
/// / HD, smooth
//                g.InterpolationMode = System. Drawing. Drawing2D. InterpolationMode. High;
//                g.SmoothingMode = System. Drawing. Drawing2D. SmoothingMode. HighQuality;
//                g.Clear(Color.Black);
//                g.DrawImage(
//                    sourceImage,
//                    new System. Drawing. Rectangle(0, 0, smallWidth, smallHeight),
//                    new System. Drawing. Rectangle(0, 0, width, height),
//                    System. Drawing. GraphicsUnit. Pixel);
//            }
/// / create a new drawing board and draw the middle drawing with the size of thumbnail
//            using (System.Drawing.Image bitmap1 = new System.Drawing.Bitmap(intThumbWidth, intThumbHeight))
//            {
/// / draw thumbnails
//                using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap1))
//                {
/// / HD, smooth
//                    g.InterpolationMode = System. Drawing. Drawing2D. InterpolationMode. High;
//                    g.SmoothingMode = System. Drawing. Drawing2D. SmoothingMode. HighQuality;
//                    g.Clear(Color.Black);
//                    int lwidth = (smallWidth - intThumbWidth) / 2;
)





上一篇:比较SHELL里面的大小
下一篇:RUBY ON RAILS 实用圣经阅读 (3)

使用道具 举报

Archiver|手机版|小黑屋|吾爱开源 |网站地图

Copyright 2011 - 2012 Lnqq.NET.All Rights Reserved( ICP备案粤ICP备14042591号-1粤ICP14042591号 )

关于本站 - 版权申明 - 侵删联系 - Ln Studio! - 广告联系

本站资源来自互联网,仅供用户测试使用,相关版权归原作者所有

快速回复 返回顶部 返回列表