惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Microsoft Azure Blog
Microsoft Azure Blog
Google Online Security Blog
Google Online Security Blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Simon Willison's Weblog
Simon Willison's Weblog
T
Threat Research - Cisco Blogs
C
CXSECURITY Database RSS Feed - CXSecurity.com
L
Lohrmann on Cybersecurity
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Forbes - Security
Forbes - Security
P
Palo Alto Networks Blog
Schneier on Security
Schneier on Security
S
Schneier on Security
T
Tor Project blog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
WordPress大学
WordPress大学
The Hacker News
The Hacker News
Hacker News - Newest:
Hacker News - Newest: "LLM"
罗磊的独立博客
Application and Cybersecurity Blog
Application and Cybersecurity Blog
F
Fortinet All Blogs
博客园 - 三生石上(FineUI控件)
小众软件
小众软件
C
Check Point Blog
Stack Overflow Blog
Stack Overflow Blog
Blog — PlanetScale
Blog — PlanetScale
雷峰网
雷峰网
S
Security @ Cisco Blogs
PCI Perspectives
PCI Perspectives
Spread Privacy
Spread Privacy
W
WeLiveSecurity
SecWiki News
SecWiki News
A
About on SuperTechFans
H
Help Net Security
博客园 - 司徒正美
Recent Commits to openclaw:main
Recent Commits to openclaw:main
爱范儿
爱范儿
S
Securelist
M
MIT News - Artificial intelligence
云风的 BLOG
云风的 BLOG
月光博客
月光博客
Jina AI
Jina AI
博客园 - 叶小钗
Vercel News
Vercel News
阮一峰的网络日志
阮一峰的网络日志
Recent Announcements
Recent Announcements
S
Secure Thoughts
The Cloudflare Blog
美团技术团队
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

博客园 - MSDI

报This application is currently offline……解决方法 用IIS建立高安全性Web服务器 关于缩略图的实现方式 - MSDI - 博客园 ASP返回操作必须使用可更新的查询错误4种可能 "此页的状态信息无效,可能已损坏。”的解决办法 - MSDI - 博客园 Windows 2003 的默认上传文件大小和大小限制的问题 微软Silverlight和Adobe Flash短兵相接 Returnil Virtual System Personal/Business Beta 1.70.6160 DES对称加解密方法 数据库开发总结(ADO.NET小结) 核心对象的连载(String对象) 核心对象的连载(Date对象) TD的noWrap属性使用注意事项 核心对象的连载(Math对象) 核心对象的连载(Array对象) ASP.NET2.0教程(第二章) ASP.NET2.0教程(第一章) ASP.NET2.0教程(前言) GDI+ 中发生一般性错误的解决方法
asp.net上传图片并生成缩略图
MSDI · 2006-09-28 · via 博客园 - MSDI

前台代码:

<%@ Page language="c#" Codebehind="TestWebForm.aspx.cs" AutoEventWireup="false" Inherits="Example.TestWebForm" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
    
<HEAD>
        
<title>在ASP.NET里轻松实现缩略图</title>
        
<meta content="Microsoft Visual Studio.NET 7.0" name="GENERATOR">
        
<meta content="Visual Basic 7.0" name="CODE_LANGUAGE">
        
<meta content="JavaScript" name="vs_defaultClientScript">
        
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
    
</HEAD>
    
<body MS_POSITIONING="GridLayout">
        
<asp:label id="Label1" runat="server"></asp:label>
        
<form id="Form1" method="post" encType="multipart/form-data" runat="server">
            
<INPUT type="file" name="file" width="600"><br>
            
<br>
            
<asp:Button id="Button1" runat="server"></asp:Button></form>
    
</body>
</HTML>

后台代码:

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.IO; 
using System.Drawing.Imaging;

namespace Example
{
    
/// <summary>
    
/// TestWebForm 的摘要说明。
    
/// </summary>

    public class TestWebForm : System.Web.UI.Page
    
{
        
protected System.Web.UI.WebControls.Label Label1;
        
protected System.Web.UI.WebControls.Button Button1;

        
private void Page_Load(object sender, System.EventArgs e)
        
{
            
// 在此处放置用户代码以初始化页面
            Label1.Text = "<h3>在ASP.NET里轻松实现缩略图</h3>"
            Button1.Text 
= "上载并显示缩略图"

        }


        
Web 窗体设计器生成的代码

        
private void Button1_Click(object sender, System.EventArgs e)
        
{
            HttpFileCollection MyFileColl 
= HttpContext.Current.Request.Files;   //获取客户端上载的文件(多部件 MIME 格式)集合
            HttpPostedFile MyPostedFile = MyFileColl[0];  //获取第一个文件

            
if (MyPostedFile.ContentType.ToString().ToLower().IndexOf("image"< 0
            

                Response.Write(
"无效的图形格式。"); 
                
return
            }
 
            GetThumbNail(MyPostedFile.FileName, 
100100
                MyPostedFile.ContentType.ToString(), 
false, MyPostedFile.InputStream); 
        }


        
/// <summary>
        
/// 得到图片的类型
        
/// </summary>
        
/// <param name="strContentType"></param>
        
/// <returns></returns>

        private System.Drawing.Imaging.ImageFormat GetImageType(object strContentType) 
        

            
if ((strContentType.ToString().ToLower()) == "image/pjpeg"
            

                
return System.Drawing.Imaging.ImageFormat.Jpeg; 
            }
 
            
else if ((strContentType.ToString().ToLower()) == "image/gif"
            

                
return System.Drawing.Imaging.ImageFormat.Gif; 
            }
 
            
else if ((strContentType.ToString().ToLower()) == "image/bmp"
            

                
return System.Drawing.Imaging.ImageFormat.Bmp; 
            }
 
            
else if ((strContentType.ToString().ToLower()) == "image/tiff"
            

                
return System.Drawing.Imaging.ImageFormat.Tiff; 
            }
 
            
else if ((strContentType.ToString().ToLower()) == "image/x-icon"
            

                
return System.Drawing.Imaging.ImageFormat.Icon; 
            }
 
            
else if ((strContentType.ToString().ToLower()) == "image/x-png"
            

                
return System.Drawing.Imaging.ImageFormat.Png; 
            }
 
            
else if ((strContentType.ToString().ToLower()) == "image/x-emf"
            

                
return System.Drawing.Imaging.ImageFormat.Emf; 
            }
 
            
else if ((strContentType.ToString().ToLower()) == "image/x-exif"
            

                
return System.Drawing.Imaging.ImageFormat.Exif; 
            }
 
            
else if ((strContentType.ToString().ToLower()) == "image/x-wmf"
            

                
return System.Drawing.Imaging.ImageFormat.Wmf; 
            }
 
            
else 
            

                
return System.Drawing.Imaging.ImageFormat.MemoryBmp; 
            }
 
        }
 

        
/// <summary>
        
/// 得到缩略图
        
/// </summary>
        
/// <param name="strFileName">文件名</param>
        
/// <param name="iWidth">宽度</param>
        
/// <param name="iheight">高度</param>
        
/// <param name="strContentType"></param>
        
/// <param name="blnGetFromFile">是否从文件创建Image对象</param>
        
/// <param name="ImgStream"></param>

        private void GetThumbNail(string strFileName, int iWidth, int iheight, 
            
string strContentType, bool blnGetFromFile, System.IO.Stream ImgStream) 
        

            System.Drawing.Image oImg;
        
            
if (blnGetFromFile) 
            

                oImg 
= System.Drawing.Image.FromFile(strFileName); 
            }
 
            
else 
            

                oImg 
= System.Drawing.Image.FromStream(ImgStream); 
            }
 
            oImg 
= oImg.GetThumbnailImage(iWidth, iheight, null, IntPtr.Zero);   //GetThumbnailImage方法是返回此Image对象的缩略图
            string strGuid = System.Guid.NewGuid().ToString().ToUpper();  //初始化 Guid 类的新实例
            string strFileExt = strFileName.Substring(strFileName.LastIndexOf("."));  //得到图片的后缀
            Response.ContentType = strContentType; 
            MemoryStream MemStream 
= new MemoryStream(); //创建其支持存储区为内存的流
            oImg.Save(MemStream, GetImageType(strContentType)); 
            MemStream.WriteTo(Response.OutputStream);  
//WriteTo方法是将此内存流的整个内容写入另一个流中
        }
 
    }

}