/// <summary> /// 保存文件 返回对象 /// </summary> /// <param name="file"></param> /// <returns></returns> public UserPictureFile SavePostedFile(HttpPostedFile file) { byte[] btImgs = new byte[file.ContentLength]; file.InputStream.Read(btImgs, 0, btImgs.Length); ///压缩图片 System.Drawing.Image original_image = System.Drawing.Image.FromStream(file.InputStream); //使用流创建图片 int width = original_image.Width; //图片的宽度 int height = original_image.Height; //图片的高度 ComputWidthHeight(ref width, ref height); System.Drawing.Image newImg = PictureFileConverter.GetThumbnailImage("", width, height, file.InputStream); byte[] newBytes = PictureFileConverter.ImageToByteArray(newImg, System.Drawing.Imaging.ImageFormat.Jpeg); string fileBase64Str = Convert.ToBase64String(newBytes); newImg.Dispose(); //释放 file.InputStream.Dispose(); //释放 original_image.Dispose(); //释放 ///压缩图片 结束 UserPictureFile model = new UserPictureFile(); model.FileContent = fileBase64Str; model.Extension = file.ContentType; model.FileName = file.FileName; model.OperatorId = string.Empty; model.PictureFileNo = DateTime.Now.ToString("yyyyMMddHHmmssfff") + ThreadSafeRandom.Next(1000).ToString("000"); InsertPicFile(model); return(model); }
public void ProcessRequest(HttpContext context) { int width = Convert.ToInt32(context.Request.QueryString["width"]); int height = Convert.ToInt32(context.Request.QueryString["height"]); string pictureFileNo = context.Request.QueryString["pictureFileNo"].ToString(); string type = context.Request.QueryString["type"].ToString(); //V2版 CDN图片 //string picUrl = ServicePic.ResolveUGCImage(GetType(type), pictureFileNo, width, height); //context.Response.Redirect(picUrl, true); #region V1版读取--直接读取数据库中的二进制 CommonService service = new CommonService(); string extension = string.Empty; Image outImage = service.GetPic(width, height, pictureFileNo, type, out extension); context.Response.ContentType = "image/jpeg"; context.Response.Clear(); context.Response.BufferOutput = true; if (null == outImage) { //输出默认图片 Bitmap defalut = PictureFileConverter.GetDefalutPic(width, height); defalut.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg); context.Response.End(); } switch (extension) { case "jpg": outImage.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg); break; case "gif": outImage.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Gif); break; case "png": outImage.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Png); break; default: outImage.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg); break; } context.Response.End(); #endregion }
public ImageModel GetPic(int width, int height, string pictureFileNo, string type) { //如果图片不存在的错误---- string filecontent = string.Empty; string picExtension = string.Empty; try { switch (type) { case "1": ProductPictureFile model = DapperUtil.QueryByIdentityWithNoLock <ProductPictureFile>(pictureFileNo); filecontent = model.FileContent; picExtension = model.Extension; break; case "2": SystemPictureFile model2 = DapperUtil.QueryByIdentityWithNoLock <SystemPictureFile>(pictureFileNo); filecontent = model2.FileContent; picExtension = model2.Extension; break; case "3": UserPictureFile model3 = DapperUtil.QueryByIdentityWithNoLock <UserPictureFile>(pictureFileNo); filecontent = model3.FileContent; picExtension = model3.Extension; break; } } catch (Exception) { return(new ImageModel()); } Image outImage = PictureFileConverter.GetThumbnailImage(filecontent, width, height); ImageModel imgModel = new ImageModel { Extension = picExtension, Image = outImage }; return(imgModel); }
public void ProcessRequest(HttpContext context) { int width = Convert.ToInt32(context.Request.QueryString["width"]); int height = Convert.ToInt32(context.Request.QueryString["height"]); string pictureFileNo = context.Request.QueryString["pictureFileNo"].ToString(); string type = context.Request.QueryString["type"].ToString(); CommonService service = new CommonService(); string extension = string.Empty; string cacheKey = string.Format("PicRead_{0}_{1}_{2}_{3}", pictureFileNo, width, height, type); Image outImage; ImageModel imageModel = (ImageModel)HttpRuntime.Cache.Get(cacheKey); if (imageModel == null) { imageModel = service.GetPic(width, height, pictureFileNo, type); if (imageModel != null) { HttpRuntime.Cache.Add(cacheKey, imageModel, null, DateTime.Now.AddMinutes(15), Cache.NoSlidingExpiration, CacheItemPriority.Normal, null); } } outImage = imageModel.Image; switch (imageModel.Extension) { case ".jpg": context.Response.ContentType = "image/jpeg"; break; case ".gif": context.Response.ContentType = "image/gif"; break; case ".png": context.Response.ContentType = "image/png"; break; default: context.Response.ContentType = "image/jpeg"; break; } context.Response.Clear(); context.Response.BufferOutput = true; if (null == outImage)//输出默认图片--图片不存在时 { Bitmap defalut = PictureFileConverter.GetDefalutPic(width, height); defalut.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg); context.Response.End(); } switch (imageModel.Extension) { case ".jpg": outImage.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg); break; case ".gif": outImage.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Gif); break; case ".png": using (MemoryStream ms = new MemoryStream()) { outImage.Save(ms, ImageFormat.Png); ms.WriteTo(context.Response.OutputStream); } //outImage.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Png); break; default: outImage.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg); break; } context.Response.End(); }