2015-09-06 69 views
1

我想获取图片以填充图片框,但不留下任何空格。因此,当图像的长宽比不调整时,剪切图像的部分以适合它。并在用户调整窗口/图片框大小时进行调整。现有选项Sizemode = Zoom会留下空白,因为它不敢切断任何图像,并且会延伸图像,使图像变形。调整图片的大小以填充图片框而不拉伸

我能想到的唯一方法是创建一个算法来调整图像的大小,保持对比度,并将图像的宽度或长度设置为pictureBox的宽度或长度,并创建一些运行时环路,算法一次一帧。它看起来有点性能沉重,它的作用和种类很诡异。有更好的选择吗?

编辑: 对于没有人来用,我实现了伊万Stoev的解决方案略有不同:

class ImageHandling 
{ 
    public static Rectangle GetScaledRectangle(Image img, Rectangle thumbRect) 
    { 
     Size sourceSize = img.Size; 
     Size targetSize = thumbRect.Size; 
     float scale = Math.Max((float) targetSize.Width/sourceSize.Width, (float) targetSize.Height/sourceSize.Height); 
     var rect = new RectangleF(); 
     rect.Width = scale * sourceSize.Width; 
     rect.Height = scale * sourceSize.Height; 
     rect.X = (targetSize.Width - rect.Width)/2; 
     rect.Y = (targetSize.Height - rect.Height)/2; 
     return Rectangle.Round(rect); 
    } 

    public static Image GetResizedImage(Image img, Rectangle rect) 
    { 
     Bitmap b = new Bitmap(rect.Width, rect.Height); 
     Graphics g = Graphics.FromImage((Image) b); 
     g.InterpolationMode = InterpolationMode.HighQualityBicubic; 
     g.DrawImage(img, 0, 0, rect.Width, rect.Height); 
     g.Dispose(); 
     try 
     { 
      return (Image)b.Clone(); 
     } 
     finally 
     { 
      b.Dispose(); 
      b = null; 
      g = null; 
     } 
    } 

    public Form1() 
    { 
     InitializeComponent(); 
     updateMainBackground(); 
    } 

    void updateMainBackground() 
    { 
     Image img = Properties.Resources.BackgroundMain; 
     Rectangle newRect = ImageHandling.GetScaledRectangle(img, mainBackground.ClientRectangle); 
     mainBackground.Image = ImageHandling.GetResizedImage(img, newRect); 
    } 

    private void Form1_Resize(object sender, EventArgs e) 
    { 
     updateMainBackground(); 
    } 
} 

回答

2

如果我理解正确,您正在寻找“填充”模式(类似于Windows背景图片)。有这样做的没有标准的方式,但它并不难,使自己以较小运算和GDI +的帮助:

using System; 
using System.Drawing; 
using System.IO; 
using System.Net; 
using System.Windows.Forms; 

namespace Samples 
{ 
    public class ImageFillBox : Control 
    { 
     public ImageFillBox() 
     { 
      SetStyle(ControlStyles.Selectable | ControlStyles.SupportsTransparentBackColor, false); 
      SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer | ControlStyles.Opaque | ControlStyles.UserPaint | ControlStyles.ResizeRedraw, true); 
     } 
     private Image image; 
     public Image Image 
     { 
      get { return image; } 
      set 
      { 
       if (image == value) return; 
       image = value; 
       Invalidate(); 
      } 
     } 
     protected override void OnPaint(PaintEventArgs e) 
     { 
      base.OnPaint(e); 
      if (image == null) 
       e.Graphics.Clear(BackColor); 
      else 
      { 
       Size sourceSize = image.Size, targetSize = ClientSize; 
       float scale = Math.Max((float)targetSize.Width/sourceSize.Width, (float)targetSize.Height/sourceSize.Height); 
       var rect = new RectangleF(); 
       rect.Width = scale * sourceSize.Width; 
       rect.Height = scale * sourceSize.Height; 
       rect.X = (targetSize.Width - rect.Width)/2; 
       rect.Y = (targetSize.Height - rect.Height)/2; 
       e.Graphics.DrawImage(image, rect); 
      } 
     } 
    } 
    static class Test 
    { 
     static void Main() 
     { 
      Application.EnableVisualStyles(); 
      Application.SetCompatibleTextRenderingDefault(false); 
      var testForm = new Form(); 
      testForm.Controls.Add(new ImageFillBox 
      { 
       Dock = DockStyle.Fill, 
       Image = GetImage(@"http://www.celebrityrockstarguitars.com/rock/images/Metall_1.jpg") 
      }); 
      Application.Run(testForm); 
     } 
     static Image GetImage(string path) 
     { 
      var uri = new Uri(path); 
      if (uri.IsFile) return Image.FromFile(path); 
      using (var client = new WebClient()) 
       return Image.FromStream(new MemoryStream(client.DownloadData(uri))); 
     } 
    } 
} 
+0

完美的作品,非常感谢。 – daboss42

+0

你有没有java的方法?谢谢 –

0

根据您可以指定PictureBoxSizeMode.Zoom以获取图像,以保持其高宽比PictureBoxSizeMode文档。它将尽可能地放大,而不会使图像的任何部分溢出图片框。

而且您可以使用Dock属性(设置DockStyle.Full)来获取图片框以调整其容器大小。