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

推荐订阅源

大猫的无限游戏
大猫的无限游戏
S
SegmentFault 最新的问题
The Cloudflare Blog
博客园_首页
WordPress大学
WordPress大学
有赞技术团队
有赞技术团队
人人都是产品经理
人人都是产品经理
爱范儿
爱范儿
罗磊的独立博客
酷 壳 – CoolShell
酷 壳 – CoolShell
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Jina AI
Jina AI
美团技术团队
博客园 - 【当耐特】
博客园 - Franky
T
Tailwind CSS Blog
雷峰网
雷峰网
小众软件
小众软件
阮一峰的网络日志
阮一峰的网络日志
Apple Machine Learning Research
Apple Machine Learning Research
Last Week in AI
Last Week in AI
V
V2EX
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
博客园 - 三生石上(FineUI控件)

博客园 - 小飞虫

遍历 Session,Cookie,Application JS获取URL参数值 - 小飞虫 - 博客园 PHP时间戳 Mysql 备注数据库脚本 gridview 为CommandArgument绑定行号 .net 3.5 属性简写 有效解决CSS兼容性的技巧 DropDownList绑定枚举类型 如何避免DropDownList绑定无效值时抛出异常 网上找的JS身份证验证,修复了一个日期验证的bug 几个js的正则表达式验证 通过javascript修改文本框内容后提交 正则表达表手机号码 修改成员管理默认的密码规则 js密码验证 js邮件地址验证脚本 .net2.0邮件发送程序 从框架中关闭浏览器的JS脚本 关于web.config的问题
asp.net 上传图片并生成缩略图
小飞虫 · 2007-01-10 · via 博客园 - 小飞虫

    bool fileOK = false;
        
string path = Server.MapPath("~/Upload")+"\\";      //上传路径
        
if (fupImgUrl.HasFile)
        
{
            
string fileExtension = System.IO.Path.GetExtension(fupImgUrl.FileName).ToLower();   //获取要上传文件的扩展名

            
string[] allowedExtensions =".gif"".png"".jpeg"".jpg" };
            
for (int i = 0; i < allowedExtensions.Length; i++)
            
{
                
if (fileExtension == allowedExtensions[i])
                
{
                    fileOK 
= true;
                }

            }

        }


        
string fileName = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() +DateTime.Now.Hour.ToString()+ DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + System.IO.Path.GetExtension(fupImgUrl.FileName).ToLower();   //生成随机的文件名
        if (fileOK)
        
{
            
try
            
{
                fupImgUrl.PostedFile.SaveAs(path 
+ fileName);  //上传原图
                System.Drawing.Image image, sImage;
                image 
= System.Drawing.Image.FromStream(fupImgUrl.PostedFile.InputStream);
                
int width = image.Width;
                
int height = image.Height;
                
int NewWidth, NewHeight;
                
if (width > height)
                
{
                    NewWidth 
= 150;
                    NewHeight 
= NewWidth*height / width;
                }

                
else
                
{
                    NewHeight 
= 150;
                    NewWidth 
= NewHeight*width / height;
                }

                sImage 
= image.GetThumbnailImage(NewWidth, NewHeight, null, IntPtr.Zero);
                
int x, y;
                x 
= sImage.Width / 2 - 30;
                y 
= sImage.Height - 20;
                System.Drawing.Bitmap output 
= new System.Drawing.Bitmap(sImage);
                System.Drawing.Graphics g
= System.Drawing.Graphics.FromImage(output);
                System.Drawing.Font Fonts 
= new System.Drawing.Font("Courier New"9);
                g.DrawString(
"水印内容。。。。", Fonts, new System.Drawing.SolidBrush(System.Drawing.Color.Red), x, y);
                output.Save(path 
+ "s\\" + fileName, System.Drawing.Imaging.ImageFormat.Jpeg);

                Response.Write(
"<script>alert('上传文件成功')</script>");
                lbFileName.Text 
="~/upload/"+fileName;
                lbImages.Text 
= "~/upload/s/" + fileName;
            }

            
catch (Exception ex)
            
{
                Response.Write(
"<script>alert('"+ex.Message+"')</script>");
            }

        }

        
else
        
{
            Response.Write(
"<script>alert('无效的文件类型')</script>");
        }