c# - 如何将图像右对齐,底部在图片框中

标签 c# winforms image

我的 picturebox1 比我要加载的图像大得多。我想要做的是将此图像对齐到右侧,并将图片框的底部对齐,如屏幕截图所示: enter image description here

编辑:工作

 private void FromCameraPictureBox_Paint(object sender, PaintEventArgs e)
    {

        if (loadimage == true)
        {
            var image = new Bitmap(@"image.jpg");
            if (image != null)
            {
                var g = e.Graphics;
                // -- Optional -- //
                g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                // -- Optional -- //
                g.DrawImage(image,
                    FromCameraPictureBox.Width - image.Width,  // to right
                    FromCameraPictureBox.Height - image.Height, // to bottom
                    image.Width,
                    image.Height);
            }

       }
       loadimage = false;
    }

现在我想从按钮触发 paintevent:

void TestButtonClick(object sender, EventArgs e)
    {

        loadimage = true;
    }

如何做到这一点?

最佳答案

我很困惑为什么这段代码工作起来很糟糕:

private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
    var g = e.Graphics;
    g.DrawImage(pictureBox1.Image, pictureBox1.Width - pictureBox1.Image.Width, pictureBox1.Height - pictureBox1.Image.Height);
}

编辑:

好的,现在可以了:

private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
    var image = Properties.Resources.SomeImage; //Import via Resource Manager
    //Don't use pictureBox1.Image property because it will
    //draw the image 2 times.
    //Make sure the pictureBox1.Image property is null in Deisgn Mode
    if (image != null)
    {
        var g = e.Graphics;
        // -- Optional -- //
        g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
        // -- Optional -- //
        g.DrawImage(image, 
            pictureBox1.Width - image.Width,  // to right
            pictureBox1.Height - image.Height, // to bottom
            image.Width,
            image.Height);
    }
}

更新:

Working :) But is there any possibility to use this code without PaintEventsArgs ? I was trying to add to my button flag and in paint (if (flag==true) then execute Your code, but it doesn't do anything - no drawing on picturebox1

那是因为 Paint 事件触发了一次。我们需要让它重绘。控件的默认重绘方法是 Refresh();

给你:

bool flag = false;
Bitmap image = null;
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
    if (flag && image != null)
    {
        var g = e.Graphics;
        // -- Optional -- //
        g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
        // -- Optional -- //
        g.DrawImage(image,
            pictureBox1.Width - image.Width,
            pictureBox1.Height - image.Height,
            image.Width,
            image.Height);
    }
}

private void button2_Click(object sender, EventArgs e)
{
    image = new Bitmap("someimage.png");
    flag = true;
    pictureBox1.Refresh(); //Causes it repaint.
}

//If you resize the form (and anchor/dock the picturebox) 
//or just resize the picturebox then you will need this:
private void pictureBox1_Resize(object sender, EventArgs e)
{
    pictureBox1.Refresh();
}

关于c# - 如何将图像右对齐,底部在图片框中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7174910/

相关文章:

html - 图像不工作

C# Blazor客户端从url中读取hash参数

c# - 更改枚举菜单名称格式 [C#]

c# - 替换 ListView 中列下的项目文本

c# - 如果我替换 PictureBox 控件中的图像,我应该先处理原始图像吗?

c# - 性能 : Returning multiple tables from Sql Server. C# .Net

python - 是否可以在 background-image 属性中使用从 context_dict 传下来的图像? Django

C#如何将图像保存到SD卡?

c# - Response.Redirect() 将绝对 URL 作为相对 URL 处理

c# - 将 List<Object> 转换为未知类型