帮我画线,而不用在picturebox中制作曲目直到mouseup [英] Help me to draw line without making a track in picturebox till mouseup

查看:68
本文介绍了帮我画线,而不用在picturebox中制作曲目直到mouseup的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨...

我只是有一些大问题......

我想在一个已经装满图片的图片框上划线。 。

我已经尝试了很多方法而且它一直都失败了



我试图保存并加载文件....但是在几分钟内有一个弹出说参数无效。



我试图制作一个Bitmap变量......然后我将它保存到该变量中...当mousedown活动时,每个鼠标移动我只是将位图varibale复制到picturebox1并再次绘制...在几分钟内出现弹出窗口并说内存不足....





你能给我一个这个问题的建议吗?

Hi...
I just have some big problem...
I want to draw line on a picturebox that''s already filled with picture...
I have already tried many methods and it kept failed

I have tried to save and load in a file.... But in several minutes there is a pop up said parameter is not valid.

I have tried to make a Bitmap variable ... and I save it into that variable when mouseup... Every mousemove when mousedown active i just copied that bitmap varibale to picturebox1 and draw again... in several minutes there is pop up appeared and said out of memory....


Can you give me a suggest for this problem?

推荐答案

这不是很难,但是你已经开始犯错:使用 PictureBox 。它根本不会帮助你,只会增加一些麻烦。我会解释怎么做。请看我过去的答案:



如何从旧图纸中清除面板 [ ^ ],

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

在图片框中添加图片 [ ^ ]。



有关渲染的更多有用细节,另请参阅:

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

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

什么样的俏皮方法是Paint? (DataGridViewImageCell.Paint(...)) [ ^ ],

如何加速我的vb.net应用程序? [ ^ ]。



现在,你需要定义渲染方法(例如,在<$ c中) $ c> OnPaint ,请参阅上面引用的答案),首先使用 System.Drawing.Graphics.DrawImage 绘制位图,然后绘制无论你想在它上面绘制什么。原则上,如果你想要像自由手绘图这样的东西,这是一种罕见的情况,你可以尝试在 Graphics 的创建实例> MouseMove 事件,但是,为了稍后保留绘图,您仍然需要将笔划绘制为数据,最终将用于在渲染方法中绘制它。



请参阅:

http ://msdn.microsoft.com/en-us/library/system.drawing.graphics.aspx [ ^ ]。



-SA
This is not to hard to do, but you already started with the mistake: using PictureBox. It won''t help you at all, will only add some hassles. I''ll explain what to do. Please see my past answers:

How do I clear a panel from old drawing[^],
draw a rectangle in C#[^],
Append a picture within picturebox[^].

For some more useful detail on rendering, see also:
Drawing Lines between mdi child forms[^],
capture the drawing on a panel[^],
What kind of playful method is Paint? (DataGridViewImageCell.Paint(...))[^],
How to speed up my vb.net application?[^].

Now, it your case, you have to define the rendering method (say, in OnPaint, please see the answers referenced above), where you first draw a bitmap using System.Drawing.Graphics.DrawImage and then draw whatever you want to draw on top of it. In principle, if you want something like free-hand drawing, this is a rare case when you can try to draw on created instance of Graphics immediately in the MouseMove event, but, to persist the drawing later, you still need to "draw" your strokes into data which finally will be used to draw it in rendering method.

Please see:
http://msdn.microsoft.com/en-us/library/system.drawing.graphics.aspx[^].

—SA


即使使用PictureBox也不太困难:

要在Picture Box中的现有图像上绘制一条线,只需这样做:

It''s not actually too difficult, even when using a PictureBox:
To draw a line on an existing image in a Picture Box, just do this:
using (Graphics g = Graphics.FromImage(myPictureBox.Image))
    {
    g.DrawLine(Pens.Red, 0, 0, 100, 100);
    }
myPictureBox.Invalidate();

这不会影响图像源 - 所以如果你想保存你的更改,你需要使用:

This does not affect the image source - so if you want to save your changes, you need to use:

myPictureBox.Image.Save(@"D:\Temp\MyModifiedPicture.jpg", ImageFormat.Jpeg);

在某些时候。







即使是图片框已经有了对象的Paint事件,对吗?......但是我仍然没有得到使用面板进行跟踪之后我怎么能...你介意给一个做的例子....

感谢您的快速回复...

感谢




这并不困难:

at some point.



"even picturebox already have Paint event of the object,right?... But I still don''t get how can I after a tracking using panel... do you mind to give an example to do ....
thanks for your fast reply...
thank"


It''s not difficult:

private Point startCoord;
private Point lastCoord;
private bool drawBox = false;
private void myPanel_MouseDown(object sender, MouseEventArgs e)
    {
    startCoord = e.Location;
    lastCoord = e.Location;
    drawBox = true;
    myPanel.Invalidate();
    }
private void myPanel_MouseMove(object sender, MouseEventArgs e)
    {
    if (drawBox)
        {
        lastCoord = e.Location;
        myPanel.Invalidate();
        }
    }
private void myPanel_MouseUp(object sender, MouseEventArgs e)
   {
    drawBox = false;
    myPanel.Invalidate();
    }
private void myPanel_Paint(object sender, PaintEventArgs e)
    {
    Graphics g = e.Graphics;
    if (drawBox)
        {
        int width = Math.Abs(lastCoord.X - startCoord.X);
        int height = Math.Abs(startCoord.Y - lastCoord.Y);
        g.DrawRectangle(Pens.Red, new Rectangle(startCoord.X, startCoord.Y, width, height));
        }
    }

你需要努力,因为它很差 - 你需要让它在所有四个象限中工作甚至没有尝试过!

You''ll need to work on that because it''s pretty poor - you need to make it work in all four quadrants which I didn''t even try for!


这篇关于帮我画线,而不用在picturebox中制作曲目直到mouseup的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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