使用C#和windows窗体中的一些像素创建一个位图 [英] Make a bitmap with some pixels in C# and windows form
本文介绍了使用C#和windows窗体中的一些像素创建一个位图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您好。我在字节数组中有一些像素。
我想创建一个位图(或者其他),这些像素显示在一个图片框中;
我不是想要使用wpf应用程序。我想使用windows表单应用程序;
请帮帮我;
谢谢
Hi. I have some pixels in an byte array.
I want create a bitmap ( or sth else) with these pixels to show in a picture box;
I don''t want use wpf application . I want use windows form application;
Please help me;
Thanks
推荐答案
我搜索了CodeProject文章本文
I searched CodeProject Articles with this text
Quote:
windows形成图片框字节数组
windows forms picture box byte array
首先,不要使用PictureBox
用于此目的。即使你可以,它根本不会帮助你,只会增加一些麻烦,吃掉一些额外的资源,不给任何回报。这个控件只是一个非常简单的案例的快捷方式。你需要立即绘制一些控件(一个自定义的控件来自System.Windows.Forms.Control
,Panel
或类似)。我将解释如何:
如何从旧图纸中清除面板 [ ^ ],
在C#中绘制一个矩形 [ ^ ],
在图片框中附加图片 [ ^ ]。
另见过去的答案:
在mdi子表单之间绘制线条[ ^ ],
在面板上捕获图纸 [ ^ ],
什么样的俏皮方法是Paint? (DataGridViewImageCell.Paint(...)) [ ^ ],
如何加速我的vb.net应用程序? [ ^ ]。
现在当它清楚时,您可以选择:您可以立即绘制像素OnPaint
(请参阅下文),或者您可能需要在位图
上执行此操作。你甚至可以用同样的方法制作两者。您只需要抽象出System.Drawing.Graphics
的实例,使其成为绘图函数的参数。你将从位图的OnPaint
的事件参数中获取参数 - 这样:
http://msdn.microsoft.com/en-us/library/system.drawing.graphics.fromimage.aspx [ ^ ]。
现在,问题可能是性能。如果你真的需要使用SetPixel
逐像素地绘制图像,并且有很多像素,它会非常慢,不要这样做!在这种情况下,请始终使用位图。 唯一的快捷方式使用System.Drawing.Bitmap.LockBits
:
http://msdn.microsoft.com/en-us/library/system.drawing.bitmap.lockbits.aspx [ ^ ]。
-SA
First of all, don''t usePictureBox
for such purpose. Even though you can, it won''t help you at all, will only add some hassles and eat up a bit of extra resources, giving nothing in return. This control in nothing but a shortcut for very simple cases. You need to draw immediately on some control (a custom one derived fromSystem.Windows.Forms.Control
,Panel
or the like). I''ll explain how:
How do I clear a panel from old drawing[^],
draw a rectangle in C#[^],
Append a picture within picturebox[^].
See also these past answers:
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 when it''s clear, you have a choice: you can draw your pixels immediately inOnPaint
(please see below), or you might need to do it onBitmap
. You can even make both in the same method. You only need to abstract out the instance ofSystem.Drawing.Graphics
, make it a parameter of drawing function. You will take it parameter from event args, in case ofOnPaint
, of from bitmap — this way:
http://msdn.microsoft.com/en-us/library/system.drawing.graphics.fromimage.aspx[^].
Now, the problem could be performance. If you really need to draw the image pixel-by-pixel usingSetPixel
, and there are many pixels, it will be prohibitively slow, don''t do it! In this case, always use bitmap. The only fast way is usingSystem.Drawing.Bitmap.LockBits
:
http://msdn.microsoft.com/en-us/library/system.drawing.bitmap.lockbits.aspx[^].
—SA
这篇关于使用C#和windows窗体中的一些像素创建一个位图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文