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

推荐订阅源

GbyAI
GbyAI
Blog — PlanetScale
Blog — PlanetScale
The GitHub Blog
The GitHub Blog
Microsoft Security Blog
Microsoft Security Blog
I
InfoQ
A
About on SuperTechFans
T
The Blog of Author Tim Ferriss
D
DataBreaches.Net
L
LangChain Blog
F
Fortinet All Blogs
C
Check Point Blog
Google DeepMind News
Google DeepMind News
云风的 BLOG
云风的 BLOG
Engineering at Meta
Engineering at Meta
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
H
Help Net Security
J
Java Code Geeks
月光博客
月光博客
H
Hackread – Cybersecurity News, Data Breaches, AI and More
IT之家
IT之家
aimingoo的专栏
aimingoo的专栏
小众软件
小众软件
宝玉的分享
宝玉的分享
Jina AI
Jina AI

博客园 - 小飞虫

遍历 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>");
        }