protected void SetWallpaper(Image wall, WallpaperMode mode, int percentTopBottomCutAllowed, int percentLeftRightCutAllowed) { bool CanBeSnapped(int width, int height) { var minRatio = TargetArea.Size.ToMinRatio(percentTopBottomCutAllowed); var maxRatio = TargetArea.Size.ToMaxRatio(percentLeftRightCutAllowed); var imageRatio = 1.0 * width / height; return(imageRatio <= maxRatio && imageRatio >= minRatio); } if (wall == null) { throw new NullReferenceException("Wallpaper can´t be null"); } switch (mode) { case WallpaperMode.AllowFill: { if (CanBeSnapped(wall.Width, wall.Height)) { SetSnappedWallpaper(wall); } else { SetPicture(wall); } break; } case WallpaperMode.AllowFillForceCut: { if (CanBeSnapped(wall.Width, wall.Height)) { SetSnappedWallpaper(wall); } else { SetCutWallpaper(wall, percentTopBottomCutAllowed, percentLeftRightCutAllowed); } break; } case WallpaperMode.Fit: { SetPicture(wall); break; } case WallpaperMode.Fill: { SetSnappedWallpaper(wall); break; } default: throw new NotImplementedException(); } }
public static MemoryStream ResizePicture( Stream stream, int targetWidth, int targetHeight, WallpaperMode mode = WallpaperMode.AllowFillForceCut, ImageFormat format = null, Color?background = null) { var pip = new PictureInPicture(new Rectangle(0, 0, targetWidth, targetHeight)); pip.SetWallpaper(Image.FromStream(stream), mode, 11, 25); var image = new Bitmap(targetWidth, targetHeight); var graphic = Graphics.FromImage(image); graphic.InterpolationMode = InterpolationMode.HighQualityBicubic; graphic.SmoothingMode = SmoothingMode.HighQuality; graphic.PixelOffsetMode = PixelOffsetMode.HighQuality; graphic.CompositingQuality = CompositingQuality.HighQuality; graphic.Clear(background ?? Color.Black); pip.DrawToGraphics(graphic); var ms = new MemoryStream(); image.Save(ms, format ?? ImageFormat.Jpeg); return(ms); }
internal void SetWallpaper(Image wall, WallpaperMode mode, int percentTopBottomCutAllowed, int percentLeftRightCutAllowed) { bool CanBeSnapped(int width, int height) { var minRatio = TargetArea.Size.ToMinRatio(percentTopBottomCutAllowed); var maxRatio = TargetArea.Size.ToMaxRatio(percentLeftRightCutAllowed); var imageRatio = 1.0 * width / height; return(imageRatio <= maxRatio && imageRatio >= minRatio); } Timestamp = DateTimeOffset.Now; CurrentOriginal = wall ?? throw new NullReferenceException("Wallpaper can´t be null"); switch (mode) { case WallpaperMode.AllowFill: { if (CanBeSnapped(wall.Width, wall.Height)) { SetSnappedWallpaper(wall); } else { SetPicture(wall); } break; } case WallpaperMode.AllowFillForceCut: { if (CanBeSnapped(wall.Width, wall.Height)) { SetSnappedWallpaper(wall); } else { SetCutWallpaper(wall, percentTopBottomCutAllowed, percentLeftRightCutAllowed); } break; } case WallpaperMode.Fit: { SetPicture(wall); break; } case WallpaperMode.Fill: { SetSnappedWallpaper(wall); break; } default: throw new NotSupportedException("WallpaperMode not supported"); } }
/// <summary> /// 更新壁纸的显示模式 /// </summary> /// <param name="wallpaperMode"></param> public Config ModifyDisplayMode(WallpaperMode wallpaperMode) { WallpaperMode = wallpaperMode; return this; }