上传并生成小图
string simgUrl,bimgUrl;
try
{
string strGuid = DateTime.Now.ToShortDateString() + System.Guid.NewGuid().ToString().ToLower();
string strFileExt = GiftImgSrc.PostedFile.FileName.Substring(GiftImgSrc.PostedFile.FileName.LastIndexOf("."));
string savebimgUrl = Server.MapPath(@"../GiftsImg") + @"\bimg\" + strGuid + strFileExt.ToLower();
GiftImgSrc.PostedFile.SaveAs(savebimgUrl);
bimgUrl = strGuid + strFileExt ;
//注意这里要用: savebimgUrl
simgUrl = GetThumbNail(savebimgUrl, 150, 170, "white", GiftImgSrc.PostedFile.ContentType.ToString(), GiftImgSrc.PostedFile.InputStream);
}
catch(Exception)
{
Page.RegisterStartupScript("", @"<script>alert('上传图片与生成图形的过程出错!!\n请重试或查看是否有读写权限!!');</script>");
return;
}
try
{
string strGuid = DateTime.Now.ToShortDateString() + System.Guid.NewGuid().ToString().ToLower();
string strFileExt = GiftImgSrc.PostedFile.FileName.Substring(GiftImgSrc.PostedFile.FileName.LastIndexOf("."));
string savebimgUrl = Server.MapPath(@"../GiftsImg") + @"\bimg\" + strGuid + strFileExt.ToLower();
GiftImgSrc.PostedFile.SaveAs(savebimgUrl);
bimgUrl = strGuid + strFileExt ;
//注意这里要用: savebimgUrl
simgUrl = GetThumbNail(savebimgUrl, 150, 170, "white", GiftImgSrc.PostedFile.ContentType.ToString(), GiftImgSrc.PostedFile.InputStream);
}
catch(Exception)
{
Page.RegisterStartupScript("", @"<script>alert('上传图片与生成图形的过程出错!!\n请重试或查看是否有读写权限!!');</script>");
return;
}
private System.Drawing.Imaging.ImageFormat GetImageType(object strContentType)
{
switch(strContentType.ToString().ToLower())
{
case "image/pjpeg":
return System.Drawing.Imaging.ImageFormat.Jpeg;
case "image/gif":
return System.Drawing.Imaging.ImageFormat.Gif;
case "image/bmp":
return System.Drawing.Imaging.ImageFormat.Bmp;
case "image/tiff":
return System.Drawing.Imaging.ImageFormat.Tiff;
case "image/x-icon":
return System.Drawing.Imaging.ImageFormat.Icon;
case "image/x-png":
return System.Drawing.Imaging.ImageFormat.Png;
case "image/x-emf":
return System.Drawing.Imaging.ImageFormat.Emf;
case "image/x-exif":
return System.Drawing.Imaging.ImageFormat.Exif;
case "image/x-wmf":
return System.Drawing.Imaging.ImageFormat.Wmf;
default:
return System.Drawing.Imaging.ImageFormat.MemoryBmp;
}
}
private string GetThumbNail(string strFileName,int iWidth,int iHeight,string BgColor,string strContentType,System.IO.Stream ImgStream)
{
System.Drawing.Image oImg = System.Drawing.Image.FromFile(strFileName);
//如果图直接小于生成的上图就直接上传不重生成
if(oImg.Width < iWidth && oImg.Height < iHeight)
{
string strGuid = DateTime.Now.ToShortDateString() + System.Guid.NewGuid().ToString().ToLower();
string strFileExt = strFileName.Substring(strFileName.LastIndexOf("."));
//保存图片
string strSaveUrl = Server.MapPath(@"../GiftsImg") + @"\simg\" + strGuid + strFileExt.ToLower();
GiftImgSrc.PostedFile.SaveAs(strSaveUrl);
return strGuid + strFileExt;
}
else
{
//小图
int intwidth, intheight;
if(oImg.Width > oImg.Height)
{
if(oImg.Width>iWidth)
{
intwidth = iWidth;
intheight = (oImg.Height * iWidth) / oImg.Width;
}
else
{
intwidth = oImg.Width;
intheight = oImg.Height;
}
}
else
{
if(oImg.Height > iHeight)
{
intwidth = (oImg.Width * iHeight) / oImg.Height;
intheight = iHeight;
}
else
{
intwidth = oImg.Width;
intheight = oImg.Height;
}
}
//oImg = oImg.GetThumbnailImage(iWidth, iheight, Nothing, (New IntPtr).Zero);
Bitmap b = new Bitmap(intwidth, intheight, PixelFormat.Format24bppRgb);
Graphics g = Graphics.FromImage(b);
Color myColor;
if(BgColor ==null)
myColor = Color.FromName("white");
else
myColor = Color.FromName(BgColor);
g.Clear(myColor);
//g.DrawImage(oImg, new Rectangle((iWidth - intwidth) / 2, (iHeight - intheight) / 2, intwidth, intheight), new Rectangle(0, 0, oImg.Width, oImg.Height), GraphicsUnit.Pixel);
g.DrawImage(oImg, new Rectangle(0, 0, intwidth, intheight), new Rectangle(0, 0, oImg.Width, oImg.Height), GraphicsUnit.Pixel);
string strGuid = DateTime.Now.ToShortDateString() + System.Guid.NewGuid().ToString().ToLower();
string strFileExt = strFileName.Substring(strFileName.LastIndexOf("."));
//保存图片
string strSaveUrl = Server.MapPath(@"../GiftsImg") + @"\simg\" + strGuid + strFileExt.ToLower();
b.Save(strSaveUrl,GetImageType(strContentType));
b.Dispose();
g.Dispose();
return strGuid + strFileExt;
}
}
{
switch(strContentType.ToString().ToLower())
{
case "image/pjpeg":
return System.Drawing.Imaging.ImageFormat.Jpeg;
case "image/gif":
return System.Drawing.Imaging.ImageFormat.Gif;
case "image/bmp":
return System.Drawing.Imaging.ImageFormat.Bmp;
case "image/tiff":
return System.Drawing.Imaging.ImageFormat.Tiff;
case "image/x-icon":
return System.Drawing.Imaging.ImageFormat.Icon;
case "image/x-png":
return System.Drawing.Imaging.ImageFormat.Png;
case "image/x-emf":
return System.Drawing.Imaging.ImageFormat.Emf;
case "image/x-exif":
return System.Drawing.Imaging.ImageFormat.Exif;
case "image/x-wmf":
return System.Drawing.Imaging.ImageFormat.Wmf;
default:
return System.Drawing.Imaging.ImageFormat.MemoryBmp;
}
}
private string GetThumbNail(string strFileName,int iWidth,int iHeight,string BgColor,string strContentType,System.IO.Stream ImgStream)
{
System.Drawing.Image oImg = System.Drawing.Image.FromFile(strFileName);
//如果图直接小于生成的上图就直接上传不重生成
if(oImg.Width < iWidth && oImg.Height < iHeight)
{
string strGuid = DateTime.Now.ToShortDateString() + System.Guid.NewGuid().ToString().ToLower();
string strFileExt = strFileName.Substring(strFileName.LastIndexOf("."));
//保存图片
string strSaveUrl = Server.MapPath(@"../GiftsImg") + @"\simg\" + strGuid + strFileExt.ToLower();
GiftImgSrc.PostedFile.SaveAs(strSaveUrl);
return strGuid + strFileExt;
}
else
{
//小图
int intwidth, intheight;
if(oImg.Width > oImg.Height)
{
if(oImg.Width>iWidth)
{
intwidth = iWidth;
intheight = (oImg.Height * iWidth) / oImg.Width;
}
else
{
intwidth = oImg.Width;
intheight = oImg.Height;
}
}
else
{
if(oImg.Height > iHeight)
{
intwidth = (oImg.Width * iHeight) / oImg.Height;
intheight = iHeight;
}
else
{
intwidth = oImg.Width;
intheight = oImg.Height;
}
}
//oImg = oImg.GetThumbnailImage(iWidth, iheight, Nothing, (New IntPtr).Zero);
Bitmap b = new Bitmap(intwidth, intheight, PixelFormat.Format24bppRgb);
Graphics g = Graphics.FromImage(b);
Color myColor;
if(BgColor ==null)
myColor = Color.FromName("white");
else
myColor = Color.FromName(BgColor);
g.Clear(myColor);
//g.DrawImage(oImg, new Rectangle((iWidth - intwidth) / 2, (iHeight - intheight) / 2, intwidth, intheight), new Rectangle(0, 0, oImg.Width, oImg.Height), GraphicsUnit.Pixel);
g.DrawImage(oImg, new Rectangle(0, 0, intwidth, intheight), new Rectangle(0, 0, oImg.Width, oImg.Height), GraphicsUnit.Pixel);
string strGuid = DateTime.Now.ToShortDateString() + System.Guid.NewGuid().ToString().ToLower();
string strFileExt = strFileName.Substring(strFileName.LastIndexOf("."));
//保存图片
string strSaveUrl = Server.MapPath(@"../GiftsImg") + @"\simg\" + strGuid + strFileExt.ToLower();
b.Save(strSaveUrl,GetImageType(strContentType));
b.Dispose();
g.Dispose();
return strGuid + strFileExt;
}
}