private void ShowImage(IMAGE image) { // Create a new temporary bitmap to resize the original image // The size of this bitmap is the size of the picImage picturebox. Bitmap tempBitmap = new Bitmap(pbImage.Width, pbImage.Height, PixelFormat.Format24bppRgb); // Set the resolution of the bitmap to match the original resolution. tempBitmap.SetResolution(image.imgSource.HorizontalResolution, image.imgSource.VerticalResolution); // Create a Graphics object to further edit the temporary bitmap Graphics bmapGraphics = Graphics.FromImage(tempBitmap); bmapGraphics.Clear(Color.White); // Set the interpolationmode插值模式 since we are resizing an image here bmapGraphics.InterpolationMode = InterpolationMode.HighQualityBicubic; // Draw the original image on the temporary bitmap, resizing it using // the calculated values of targetWidth and targetHeight. bmapGraphics.DrawImage(image.imgSource , new Rectangle(image.targetLeft, image.targetTop, image.targetWidth, image.targetHeight) , new Rectangle(0, 0, image.imgSource.Width, image.imgSource.Height) , GraphicsUnit.Pixel); bmapGraphics.Dispose(); if (toRotate == true) { tempBitmap = RotateImage(tempBitmap, angle); } pbImage.Image = tempBitmap; }
private void pbImage_MouseWheel(object sender, MouseEventArgs e) { IMAGE imgToZoom = img[picnow]; double ratio; if (e.Delta > 0) { ratio = e.Delta / 60; } else { double k = e.Delta; k = -k; ratio = 80 / k; } imgToZoom.targetHeight = (int)(imgToZoom.targetHeight * ratio); imgToZoom.targetWidth = (int)(imgToZoom.targetWidth * ratio); imgToZoom.targetTop = (pbImage.Height - imgToZoom.targetHeight) / 2; imgToZoom.targetLeft = (pbImage.Width - imgToZoom.targetWidth) / 2; img[picnow] = imgToZoom; ShowImage(imgToZoom); }
private void btOpenImage_Click(object sender, EventArgs e) { hasimage = true; OpenFileDialog openFileDia = new OpenFileDialog(); if (openFileDia.ShowDialog() == DialogResult.OK) { cnt = 0; max = 0; picnow = 0; string path_Directory = openFileDia.FileName.Substring(0, openFileDia.FileName.LastIndexOf("\\")); string[] files = System.IO.Directory.GetFiles(path_Directory); foreach (string filename in files) { try { string name = filename.Substring(filename.LastIndexOf("\\"), filename.Length - filename.LastIndexOf("\\")); string filetype = filename.Substring(filename.LastIndexOf("."), filename.Length - filename.LastIndexOf(".")); if (filetype == ".jpg") { img[cnt] = new IMAGE(Image.FromFile(path_Directory + name), 0, 0, 0, 0); cnt++; if (filename == openFileDia.FileName) { picnow = cnt - 1; } } } catch { } } max = cnt; cnt = picnow; btSaveImg.Enabled = true; btLastImg.Enabled = true; btNextImg.Enabled = true; this.ResizeAndDisplayImage(img[picnow]); } }
private void ResizeAndDisplayImage(IMAGE OriginalImage) { this.pbImage.BackColor = Color.White; if (OriginalImage == null) { return; } int sourceWidth = OriginalImage.imgSource.Width; int sourceHeight = OriginalImage.imgSource.Height; double ratio; //Calculate targetWidth and targetHeight, so that the image will fit into the pictureBox if ((sourceWidth != pbImage.Width) || (sourceHeight != pbImage.Height)) { if (sourceHeight > OriginalImage.targetHeight) { if (sourceHeight > pbImage.Height || sourceWidth > pbImage.Width) { OriginalImage.targetHeight = pbImage.Height; ratio = (double)OriginalImage.targetHeight / sourceHeight; OriginalImage.targetWidth = (int)(sourceWidth * ratio); if (pbImage.Width < OriginalImage.targetWidth) { OriginalImage.targetWidth = pbImage.Width; ratio = (double)OriginalImage.targetWidth / sourceWidth; OriginalImage.targetHeight = (int)(sourceHeight * ratio); } else if (pbImage.Height < OriginalImage.targetHeight) { OriginalImage.targetHeight = pbImage.Height; ratio = (double)OriginalImage.targetHeight / sourceHeight; OriginalImage.targetWidth = (int)(sourceWidth * ratio); } } else { OriginalImage.targetHeight = sourceHeight; OriginalImage.targetWidth = sourceWidth; } } OriginalImage.targetTop = (pbImage.Height - OriginalImage.targetHeight) / 2; OriginalImage.targetLeft = (pbImage.Width - OriginalImage.targetWidth) / 2; OriginalImage.targetTop = OriginalImage.targetTop > 0 ? OriginalImage.targetTop : 0; OriginalImage.targetLeft = OriginalImage.targetLeft > 0 ? OriginalImage.targetLeft : 0; } else { // Calculate the targetTop and targetLeft values, to center the image OriginalImage.targetTop = (pbImage.Height - sourceHeight) / 2; OriginalImage.targetLeft = (pbImage.Width - sourceWidth) / 2; } ShowImage(OriginalImage); this.updateImgInfo(OriginalImage.imgSource); }