如何将图像右对齐,底部放在图片框中 [英] How to put image aligned right, bottom in picturebox

查看:120
本文介绍了如何将图像右对齐,底部放在图片框中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

i have picturebox1 much larger than image i'd like to load. What i want to do is aligned this image to right side, and bottom of picturebox like on screenshot:

编辑:工作

 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:

and now i want to fire paintevent from button:

void TestButtonClick(object sender, EventArgs e)
    {

        loadimage = true;
    }

如何做到这一点?

推荐答案

我很困惑为什么这段代码很讨厌:

I'm confused why this code works nasty:

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);
    }
}

更新:


工作:)但是否有可能在没有
PaintEventsArgs的情况下使用此代码?我试图添加到我的按钮标志和油漆
(if(flag == true)然后执行你的代码,但它没有做任何事情 -
没有绘图picturebox1

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();

That's because Paint event fires once. We need to make it redraw. The default redraw method for controls is 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();
}

这篇关于如何将图像右对齐,底部放在图片框中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆