用C#代码如何实现,当点击按钮时

用C#代码如何实现,当点击按钮时,上传一张原始图片,并对该图片进行裁剪成固定的图片大小,且数据库里面保存原始图片的路径,和裁剪后的图片的路径,在线等待,谢谢!做好给出代码... 用C#代码如何实现,当点击按钮时,上传一张原始图片,并对该图片进行裁剪成固定的图片大小,且数据库里面保存原始图片的路径,和裁剪后的图片的路径,在线等待,谢谢!做好给出代码 展开
 我来答
帐号已注销
2012-05-16 · TA获得超过345个赞
知道小有建树答主
回答量:637
采纳率:0%
帮助的人:184万
展开全部
public string UpLoadImage(HtmlInputFile upImage, string sSavePath, string sThumbExtension, int intThumbWidth, int intThumbHeight)
{
string sThumbFile = "";
string sFilename = "";

if (upImage.PostedFile != null)
{
HttpPostedFile myFile = upImage.PostedFile;
int nFileLen = myFile.ContentLength;
if (nFileLen == 0)
return "没有选择上传图片";

//获取upImage选择文件的扩展名
string extendName = System.IO.Path.GetExtension(myFile.FileName).ToLower();
//判断是否为图片格式
if (extendName != ".jpg" && extendName != ".jpge" && extendName != ".gif" && extendName != ".bmp" && extendName != ".png")
return "图片格式不正确";

byte[] myData = new Byte[nFileLen];
myFile.InputStream.Read(myData, 0, nFileLen);

sFilename = System.IO.Path.GetFileName(myFile.FileName);
int file_append = 0;
//检查当前文件夹下是否有同名图片,有则在文件名+1
while (System.IO.File.Exists(System.Web.HttpContext.Current.Server.MapPath(sSavePath + sFilename)))
{
file_append++;
sFilename = System.IO.Path.GetFileNameWithoutExtension(myFile.FileName)
+ file_append.ToString() + extendName;
}
System.IO.FileStream newFile
= new System.IO.FileStream(System.Web.HttpContext.Current.Server.MapPath(sSavePath + sFilename),
System.IO.FileMode.Create, System.IO.FileAccess.Write);
newFile.Write(myData, 0, myData.Length);
newFile.Close();

//以上为上传原图

try
{
//原图加载
using (System.Drawing.Image sourceImage = System.Drawing.Image.FromFile(System.Web.HttpContext.Current.Server.MapPath(sSavePath + sFilename)))
{
//原图宽度和高度
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;
}

//判断缩略图在当前文件夹下是否同名称文件存在
file_append = 0;
sThumbFile = sThumbExtension + System.IO.Path.GetFileNameWithoutExtension(myFile.FileName) + extendName;

while (System.IO.File.Exists(System.Web.HttpContext.Current.Server.MapPath(sSavePath + sThumbFile)))
{
file_append++;
sThumbFile = sThumbExtension + System.IO.Path.GetFileNameWithoutExtension(myFile.FileName) +
file_append.ToString() + extendName;
}
//缩略图保存的绝对路径
string smallImagePath = System.Web.HttpContext.Current.Server.MapPath(sSavePath) + sThumbFile;

//新建一个图板,以最小等比例压缩大小绘制原图
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(smallImagePath, System.Drawing.Imaging.ImageFormat.Jpeg);
}
}
}
}
}
catch
{
//出错则删除
System.IO.File.Delete(System.Web.HttpContext.Current.Server.MapPath(sSavePath + sFilename));
return "图片格式不正确";
}
//返回缩略图名称
return sThumbFile;
}
return "没有选择图片";
}
匿名用户
2012-05-16
展开全部
15分就当纯伸手党。。。。
自己一点也不思考,给你代码又有什么意思。你也看不懂。
至少你自己写个差不多,别人给人解决关键的难点还行吧。
如果一点也不懂,就先去看C#数据库方面的资料,再看bitmap、graphics类的资料。
飘过。
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
百度网友3c6443c94
2012-05-15
知道答主
回答量:11
采纳率:0%
帮助的人:3.9万
展开全部
这个你得看处理图片相应的资料。我现在不做这些,我觉得挺好做的。
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 1条折叠回答
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式