如何使用C#以另一种形式从表单中显示图像。 [英] How do I show image in a form taken from another form using C#.

查看:53
本文介绍了如何使用C#以另一种形式从表单中显示图像。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在form1中输入了一个图像,并希望以另一种形式2处理它。我必须在form2_first下面传递form1_User_Input_Image带下划线的代码行。

请帮我这样做。



Form1

I have taken input of an image in form1 and want to process it in another form2. I have to pass form1_User_Input_Image in form2_first underlined Line of code.
Please help me to do this.

Form1

//browse image in pictureBox1 Click
private void pictureBox1_Click(object sender, EventArgs e)
{
    OpenFileDialog opf = new OpenFileDialog();
    opf.Filter = "Choose Image(*.jpg; *.png; *.gif)|*.jpg; *.png; *.gif";
    if (opf.ShowDialog() == DialogResult.OK)
    {
        pictureBox1.Image = Image.FromFile(opf.FileName);
    }
}





Form2





Form2

{
Image img = Image.FromFile(img);
int widthThird = (int)((double)img.Width / 25.0 + 0.5);
int heightThird = (int)((double)img.Height / 20.0 + 0.5);
Bitmap[,] bmps = new Bitmap[20, 25];
for (int i = 0; i < 20; i++)
    for (int j = 0; j < 25; j++)
    {
        bmps[i, j] = new Bitmap(widthThird, heightThird);
        Graphics g = Graphics.FromImage(bmps[i, j]);
        g.DrawImage(img, new Rectangle(0, 0, widthThird, heightThird), new Rectangle(j * widthThird, i * heightThird, widthThird, heightThird), GraphicsUnit.Pixel);
        g.Dispose();
    }

推荐答案

如果要捕获Form1上显示的Picture到另一个PictureBox ,放在Form2上,你必须构建一个对Form1的对象引用来访问它上面的PictureBox。
If you want to catch the Picture, which is shown on Form1, to another PictureBox, which is placed on Form2, you have to build an Object-Reference to Form1 to access the PictureBox on it.


你实际上并没有重用位图。在第二种形式中,您从文件创建另一个位图。所以,关于表单之间协作的所有问题都是毫无意义的,问题没有得到解释。代码看起来像是不可接受的。您硬编码图像大小和其他数字,使用硬编码值两次,这使代码不受支持。在图像中绘制以在表单上显示图像是没有意义的。如果这是你需要的,你只需要在事件的处理程序中的某些控件(甚至是表单本身)上使用 Graphics.DrawImage Control.Paint 或覆盖方法 Control.OnPaint



如果这就是你的全部需要,你也不需要这个冗余控件 PictureBox 。 (对不起,如果这个建议是多余的;这是因为我不知道你在哪里使用它以及如何,所以,请考虑这个建议以防万一。)请看我过去的答案:

在图片框中附加图片 [ ^ ],

在C#中绘制一个矩形 [ ^ ],

如何清除面板旧图纸 [ ^ ]。



有关图形和图像渲染的更多信息,请参阅我过去的答案:

< a href =http://www.codeproject.com/Answers/176000/What-kind-of-playful-method-is-Paint havenGridViewI#answer2>什么样的俏皮方法是Paint? (DataGridViewImageCell.Paint(...)) [ ^ ],

在面板上捕获绘图 [ ^ ],

mdi子表单之间的绘制线 [ ^ ]。



表格协作问题,请参阅我的文章:一次回答的许多问题 - Windows之间的协作佛rms或WPF Windows [ ^ ]。



-SA
You are not actually reusing the bitmap. In second form, you create another bitmap from file. So, all your question about collaboration between forms is totally pointless, the problem is not explained. The code looks like something unacceptable. You hard-code the image size and other figures, use the hard-coded values twice, which makes the code unsupportable. Drawing in an image for showing the image on the form is pointless. If this is what you need, you just need to use Graphics.DrawImage on some control (even on the form itself) in the handler of the event Control.Paint or overridden method Control.OnPaint.

If that's all you need, you also don't need this redundant control PictureBox. (Sorry if this advice is redundant; this is because I don't know where do you use it and how, so, consider this advice as as "just in case" one.) Please see my past answers:
Append a picture within picturebox[^],
draw a rectangle in C#[^],
How do I clear a panel from old drawing[^].

For more information on graphics and image rendering, please see my past answers:
What kind of playful method is Paint? (DataGridViewImageCell.Paint(...))[^],
capture the drawing on a panel[^],
Drawing Lines between mdi child forms[^].

On form collaboration issues, please see my article: Many Questions Answered at Once — Collaboration between Windows Forms or WPF Windows[^].

—SA


这篇关于如何使用C#以另一种形式从表单中显示图像。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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