加载和编辑图像 [英] loading and editing an image

查看:63
本文介绍了加载和编辑图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我想加载图像并进行编辑.例如,我需要使用鼠标在图像上画一条线或擦除某物,还需要获取图片上的两点并评估它们之间的距离现在,我想知道我应该使用哪个控件,图片框,面板,...或将图像加载到表单上.考虑

SAKryukov

在页面" 图片中在solution2中所说的内容重新加载后盒子丢失图纸 [ ^ ],我完全感到困惑.您的建议是什么?如果可能的话,请解释一下我可以使用您建议的控件的方式,因为我是C#的新手.在此先感谢很多.

解决方案

我要做的方法是使用面板,并处理Paint事件. PictureBox是一个选项,但是它会使事情变得更加笨拙,因为这意味着绘制图像本身,即使是临时性的东西(例如,它们之间的距离标记的开始和结束)也是如此.使用自绘面板,操作起来容易得多.它还为您提供了用于绘制的适当图形上下文,因此您不必担心它.

在您的表单代码中,添加:

 私有图片myImage; 


在表单Load事件中,添加:

 myImage = Image.FromFile( @" ); 


将一个面板添加到您的窗体,并将其命名为myPanel.然后在属性"窗格中,选择事件",然后双击"Paint"事件.

 私有  void  myPanel_Paint(对象发​​件人,PaintEventArgs e)
    {
    图形g = e.Graphics;
    g.DrawImage(myImage,点( 0  0 ));
    } 

然后,您可以添加代码来处理鼠标,依此类推,将更改添加到Paint例程中,并在更改内容后调用myPanel.Invalidate()方法.



"非常好.当我搜索paint事件和onpaint事件时,它们会在表单更改时发生,例如,当我拖动表单时,就会发生paint事件. ?"表示使用您建议的方式,如果我拖动表格,会不会松开以前的画?"



这就是重点!
在这种情况下,因为您正在处理面板的Paint事件,所以当需要重新绘制面板而不是整个窗体时,会发生这种情况-如果窗体需要重新绘制,则面板也会得到一个.在面板上调用Invalidate会使它重新绘制(并发生Paint事件).
因此,每次使面板失效时,您在绘画事件中所做的任何事情都会被重做-因此,如果您想要从此处到此处的距离"标记,则不要在完成测量后将其留在此处:

 私有  void  myPanel_Paint(对象发​​件人,PaintEventArgs e)
    {
    图形g = e.Graphics;
    g.DrawImage(myImage,点( 0  0 ));
    如果(衡量标准)
        {
        g.DrawLine(Pens.Red,start,end);
        myLabel.Text =(Math.Sqrt(Math.Pow(end.X-start.X, 2 )+ Math.Pow(end.Y-start.Y,  2 )))).ToString();
        }
    }
私有点开始;
私有端点;
私有 布尔 measure =  false ;
私有 无效 myPanel_MouseDown(对象发​​件人,MouseEventArgs e)
    {
    如果(!措施)
        {
        开始= e.Location;
        测量=  true ;
        }
    myPanel.Invalidate();
    }

私有 无效 myPanel_MouseUp(对象发​​件人,MouseEventArgs e)
    {
    如果(衡量标准)
        {
        测量=  false ;
        myPanel.Invalidate();
        }
    }

私有 无效 myPanel_MouseMove(对象发​​件人,MouseEventArgs e)
    {
    如果(衡量标准)
        {
        结束= e.Location;
        myPanel.Invalidate();
        }
    }
}

如果要对图像进行永久"更改,则可以从中获取一个Graphics上下文,然后在该上下文上进行绘制,而不是使用Paint事件提供的上下文(记住,在完成上下文处理后再进行处置).确实,您可以通过调用Panel.DrawToBitmap方法来使面板将自身绘制到新的Image上.

请注意,源映像文件不会受到影响-您必须使用Image.Save方法来更新磁盘内容.在您的问题中引用.

在许多情况下,PictureBox是多余的.它不会为Control添加任何功能,而只会消耗资源和开发时间.它仅用于静态显示从文件或资源加载的某些图片,或者可能是固定的少量图片更改.如果您想要任何交互或动画行为(例如在您当前的问题中,要求实现交互行为).

因此,问题不仅仅是正确的.最好问一下:我为什么需要PictureBox?".请咨询建议您使用此组件的人.

我在过去的其他解决方案中说明了怎么做: http://en.wikipedia.org /wiki/GIMP [ ^ ], http://en.wikipedia.org/wiki/GIMP [Picture box lose drawing after reloading[^] ,I''m completly confused.what''s your suggestion? if possible,please explain the way I can use the control(or the way) you suggest,as I''m new in C#.thanks a lot in advance.

解决方案

The way I would do it is to use a Panel, and handle the Paint event. The PictureBox is an option, but it can make things clumsier, as it means drawing onto the image itself, even for temporary stuff such as your start and end of distance-between-them markers. With a self drawn panel, this is a lot easier to handle. It also provides you with the appropriate graphics context for drawing, so you don''t have to worry about it.

In your form code, add:

private Image myImage;


In your form Load event, add:

myImage = Image.FromFile(@"D:\Temp\MyPic.jpg");


Add a Panel to your form, and call it myPanel. Then in teh properties pane, seleect Events, and double click the Paint event.

private void myPanel_Paint(object sender, PaintEventArgs e)
    {
    Graphics g = e.Graphics;
    g.DrawImage(myImage, new Point(0, 0));
    }

You can then add your code to handle the mouse, and so forth, adding the changes to the Paint routine and calling the myPanel.Invalidate() method when you have changed things.



"that''s very good.as I searched for paint event and onpaint event, They occur whenever the form changes,for example when I drag the form, the paint event will occur.doesn''t it make a ploblem? means that by using the way you suggest ,If I drag the form ,will I loose my previous painting?"



That''s kind of the whole point!
In this case, because you are handling the Paint event for the Panel, it will happen when the panel needs to be re-drawn, not the whole form - if the form needs a a re-paint, the panel will get one as well. Calling Invalidate on the Panel causes it to be re-drawn (and the Paint event occurs).
So anything you do in your paint event will be re-done every time the panel is invalidated - so if you want a "distance from here to here" marker you don''t want it left there when you have finished measuring:

private void myPanel_Paint(object sender, PaintEventArgs e)
    {
    Graphics g = e.Graphics;
    g.DrawImage(myImage, new Point(0, 0));
    if (measure)
        {
        g.DrawLine(Pens.Red, start, end);
        myLabel.Text = (Math.Sqrt(Math.Pow(end.X - start.X, 2) + Math.Pow(end.Y - start.Y, 2))).ToString();
        }
    }
private Point start;
private Point end;
private bool measure = false;
private void myPanel_MouseDown(object sender, MouseEventArgs e)
    {
    if (!measure)
        {
        start = e.Location;
        measure = true;
        }
    myPanel.Invalidate();
    }

private void myPanel_MouseUp(object sender, MouseEventArgs e)
    {
    if (measure)
        {
        measure = false;
        myPanel.Invalidate();
        }
    }

private void myPanel_MouseMove(object sender, MouseEventArgs e)
    {
    if (measure)
        {
        end = e.Location;
        myPanel.Invalidate();
        }
    }
}

If you want "permanent" changes to the Image, then you get a Graphics context from that, and draw on that instead of the one the Paint event provided (remembering to Dispose the context when you have finished with it). Indeed, you can get the panel to draw itself onto a new Image by calling the Panel.DrawToBitmap method!

Note that the source image file will not be affected - you have to use the Image.Save method to update disk contents.


It is not clear what you did not understand from my past solution you referenced in your question.

In many cases, PictureBox is redundant. It does not add any features to Control but only eats up resources and you development time. It is only useful for statically showing of some picture, or maybe a fixed small change of pictures, loaded from a file or resource. If you want any interactive or animated behavior (like in your current question, which required implementation of interactive behavior).

So, the question is not just quite correct. It would be better to ask: "why do I need PictureBox?". Ask it to someone who advised you to use this component.

I explained what to do instead in my other past solution: How do I clear a panel from old drawing[^].

An alternative would be using WPF which has a number of benefits.

I must say that creation of the editor for pixel graphics is not easy at all. To get an idea on how much is involved, you can download the source code and GIMP and see: http://en.wikipedia.org/wiki/GIMP[^], http://en.wikipedia.org/wiki/GIMP[^].

—SA


这篇关于加载和编辑图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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