public void ShowImg(HttpContext context, Bitmap image) { Graphics graphicImage; try { graphicImage = Graphics.FromImage(image); } catch { image = ImgLib.ConvertBitmapToJpeg(image, 100); graphicImage = Graphics.FromImage(image); } //Smooth graphics is nice. graphicImage.SmoothingMode = SmoothingMode.AntiAlias; //Set the content type context.Response.ContentType = "image/jpeg"; //Save the new image to the response output stream. image.Save(context.Response.OutputStream, ImageFormat.Jpeg); //Clean house. graphicImage.Dispose(); image.Dispose(); }
public void InitImg(HttpContext context, string DisplayID, string PhotoID) { Photo myPhoto = Get_Photo(PhotoID); Photo_Setting myPhoto_Setting = Get_Photo_Setting(DisplayID); Bitmap bitMapImage; // 检测文件是否存在 if (myPhoto.ImageURL_Type == ImageURL_Type.Internal) { // 本地文件 if (File.Exists(context.Server.MapPath(myPhoto.ImageURL))) { bitMapImage = new Bitmap(context.Server.MapPath(myPhoto.ImageURL)); } else { bitMapImage = new Bitmap(context.Server.MapPath(myPhoto_Setting.Image_BrokenURL)); } } else { // 远程文件 try { bitMapImage = ImgLib.GetBitmapFromUri(myPhoto.ImageURL); } catch { bitMapImage = new Bitmap(context.Server.MapPath(myPhoto_Setting.Image_BrokenURL)); } } // Photo Setting if (myPhoto_Setting.IsResize) { Point resizePoint = new Point(); switch (myPhoto_Setting.Resize_Type) { case Resize_Type.Fixed: resizePoint = ResizeFixed(bitMapImage, myPhoto_Setting); break; case Resize_Type.MinHeight: resizePoint = ResizeMinHeight(bitMapImage, myPhoto_Setting); break; case Resize_Type.MinWidth: resizePoint = ResizeMinWidth(bitMapImage, myPhoto_Setting); break; case Resize_Type.MinHeight_and_MinWidth: resizePoint = ResizeMinHeightMinWidth(bitMapImage, myPhoto_Setting); break; case Resize_Type.MaxHeight: resizePoint = ResizeMaxHeight(bitMapImage, myPhoto_Setting); break; case Resize_Type.MaxWidth: resizePoint = ResizeMaxWidth(bitMapImage, myPhoto_Setting); break; case Resize_Type.MaxHeight_and_MaxWidth: resizePoint = ResizeMaxHeightMaxWidth(bitMapImage, myPhoto_Setting); break; } bitMapImage = ImgLib.ResizeBitmap(bitMapImage, resizePoint.X, resizePoint.Y); } if (myPhoto_Setting.IsOverlay) { Bitmap overlayBitmap = new Bitmap(context.Server.MapPath(myPhoto_Setting.Overlay_ImageURL)); Point overlayPoint = new Point(); switch (myPhoto_Setting.Overlay_Position) { case Overlay_Position.TopLeft: overlayPoint = OverlayTopLeft(bitMapImage, overlayBitmap, myPhoto_Setting); break; case Overlay_Position.TopRight: overlayPoint = OverlayTopRight(bitMapImage, overlayBitmap, myPhoto_Setting); break; case Overlay_Position.Center: overlayPoint = OverlayCenter(bitMapImage, overlayBitmap, myPhoto_Setting); break; case Overlay_Position.BottomLeft: overlayPoint = OverlayBottomLeft(bitMapImage, overlayBitmap, myPhoto_Setting); break; case Overlay_Position.BottomRight: overlayPoint = OverlayBottomRight(bitMapImage, overlayBitmap, myPhoto_Setting); break; } bitMapImage = ImgLib.OverlayBitmap(bitMapImage, overlayBitmap, myPhoto_Setting.Overlay_Opacity, overlayPoint); } ShowImg(context, bitMapImage); }