public void MakeBmp(string path, int clickX, int clickY, int widthOfResizedImage, int heightOfResizedImage, JpegRotationFinder shouldRotateImages) { Bmp.Dispose(); Bmp = new Bitmap(MaxWidth, MaxHeight, PixelFormat.Format32bppPArgb); if (path == null || !FilenameUtils.LooksLikeImage(path) || !File.Exists(path)) { return; } // we can disregard bitmapWillLockFile because we'll quickly dispose bitmapFull. using (Bitmap bitmapFull = ImageCache.GetBitmap(path, shouldRotateImages, out bool bitmapWillLockFile)) { if (bitmapFull.Width == 1 || bitmapFull.Height == 1) { return; } GetShiftAmount(bitmapFull, clickX, clickY, widthOfResizedImage, heightOfResizedImage, out int shiftX, out int shiftY); // draw the entire image, but pushed off to the side using (Graphics g = Graphics.FromImage(Bmp)) { g.FillRectangle(Brushes.White, 0, 0, MaxWidth, MaxHeight); g.DrawImageUnscaled(bitmapFull, -shiftX, -shiftY); } } }
// bitmapWillLockFile indicates whether holding onto Bitmap will hold lock on a file. public static Bitmap GetBitmap(string path, JpegRotationFinder shouldrotate, out bool bitmapWillLockFile) { Bitmap bitmap = null; try { if (ModeUtils.IsWebp(path)) { byte[] bytesData = File.ReadAllBytes(path); var decoder = new Imazen.WebP.SimpleDecoder(); bitmap = decoder.DecodeFromBytes(bytesData, bytesData.LongLength); bitmapWillLockFile = false; } else { bitmap = new Bitmap(path); bitmapWillLockFile = true; // some image files have custom resolutions, // I prefer seeing all files at the same resolution. bitmap.SetResolution(96.0f, 96.0f); } } catch (Exception e) { if (ModeUtils.IsWebp(path) && e.ToString().ToUpperInvariant().Contains("0x8007007E")) { Utils.MessageErr("It appears that the Visual C++ Redistributable " + "Packages for Visual Studio 2013 are not installed; please run " + "vcredist_x64.exe so that libwebp.dll can be used."); } Utils.MessageErr("Could not load the image " + path + Utils.NL + Utils.NL + Utils.NL + "Details: " + e.ToString(), true); if (bitmap != null) { bitmap.Dispose(); } bitmapWillLockFile = true; bitmap = new Bitmap(1, 1, PixelFormat.Format32bppPArgb); } if (shouldrotate != null && shouldrotate.ShouldRotate(path)) { bitmap.RotateFlip(RotateFlipType.Rotate90FlipNone); } return(bitmap); }
public ImageCache( int maxWidth, int maxHeight, int cacheSize, Func <Action, bool> callbackOnUiThread, Func <Bitmap, bool> canDisposeBitmap, JpegRotationFinder shouldRotateThisImage, bool tileImages) { ResizeToFit = true; ResizeFactor = 1; MaxWidth = maxWidth; MaxHeight = maxHeight; _cacheSize = cacheSize; _callbackOnUiThread = callbackOnUiThread; _canDisposeBitmap = canDisposeBitmap; _shouldRotateThisImage = shouldRotateThisImage; _tileImages = tileImages; Excerpt = new ImageViewExcerpt(maxWidth, maxHeight); }