Fit Image into PictureBox

前端 未结 9 727
栀梦
栀梦 2020-12-15 14:43
using (SqlConnection myDatabaseConnection = new SqlConnection(myConnectionString.ConnectionString))
{
    myDatabaseConnection.Open();
    using (SqlCommand SqlComma         


        
相关标签:
9条回答
  • 2020-12-15 15:37

    First off, in order to have any image "resize" to fit a picturebox, you can set the PictureBox.SizeMode = PictureBoxSizeMode.StretchImage

    If you want to do clipping of the image beforehand (i.e. cut off sides or top and bottom), then you need to clearly define what behavior you want (start at top, fill the height of the pciturebox and crop the rest, or start at the bottom, fill the height of the picturebox to the top, etc), and it should be fairly simple to use the Height / Width properties of both the picturebox and the image to clip the image and get the effect you are looking for.

    0 讨论(0)
  • 2020-12-15 15:37

    I have routine in VB ..

    but you should have 2 pictureboxes .. 1 for frame .. 1 for the image .. and it make keep the picture's size ratio

    Assumed picFrame is the image frame and picImg is the image

    Sub InsertPicture(ByVal oImg As Image)
        Dim oFoto As Image
        Dim x, y As Integer
    
        oFoto = oImg
        picImg.Visible = False
        picImg.Width = picFrame.Width - 2
        picImg.Height = picFrame.Height - 2
        picImg.Location = New Point(1, 1)
        SetPicture(picPreview, oFoto)
        x = (picImg.Width - picFrame.Width) / 2
        y = (picImg.Height - picFrame.Height) / 2
        picImg.Location = New Point(x, y)
        picImg.Visible = True
    
    End Sub
    

    I'm sure you can make it as C# ....

    0 讨论(0)
  • 2020-12-15 15:41

    The PictureBox.SizeMode options are missing a "fill" or "cover" mode which would be like zoom except with cropping to ensure you're filling the picture box. In CSS it's the "cover" option.

    This code should enable that:

    static public void fillPictureBox(PictureBox pbox, Bitmap bmp)
    {
        pbox.SizeMode = PictureBoxSizeMode.Normal;
        bool source_is_wider = (float)bmp.Width / bmp.Height > (float)pbox.Width / pbox.Height;
    
        var resized = new Bitmap(pbox.Width, pbox.Height);
        var g = Graphics.FromImage(resized);        
        var dest_rect = new Rectangle(0, 0, pbox.Width, pbox.Height);
        Rectangle src_rect;
    
        if (source_is_wider)
        {
            float size_ratio = (float)pbox.Height / bmp.Height;
            int sample_width = (int)(pbox.Width / size_ratio);
            src_rect = new Rectangle((bmp.Width - sample_width) / 2, 0, sample_width, bmp.Height);
        }
        else
        {
            float size_ratio = (float)pbox.Width / bmp.Width;
            int sample_height = (int)(pbox.Height / size_ratio);
            src_rect = new Rectangle(0, (bmp.Height - sample_height) / 2, bmp.Width, sample_height);
        }
    
        g.DrawImage(bmp, dest_rect, src_rect, GraphicsUnit.Pixel);
        g.Dispose();
    
        pbox.Image = resized;
    }
    
    0 讨论(0)
提交回复
热议问题