如何保存到位图? [英] How to save to a bitmap?

查看:70
本文介绍了如何保存到位图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码

使用System;

使用System.Windows.Forms;

使用System.Drawing;

使用System.Collections;

命名空间FINN

{


公共类flickerFreePictureBox:PictureBox

{

public ArrayList Lines = new ArrayList();

public flickerFreePictureBox()

{


this.SetStyle(ControlStyles.DoubleBuffer,true);

this.SetStyle(ControlStyles.AllPaintingInWmPaint,true);

this.SetStyle(ControlStyles.UserPaint ,true);

this.SetStyle(ControlStyles.ResizeRedraw,true);

//this.MouseMove + = new MouseEventHandler(_MouseMove);

}

protected override void OnPaint(PaintEventArgs e)

{

bool error = false;

/ /e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;

尝试

{

foreach(scribbleObject al in Lines)

{

bool Drawd ot = false;

尝试

{

Point [] linelist = new Point [

al.linePositions。计数];


al.linePositions.CopyTo(0,linelist,0,al.linePositi ons.Count);

e.Graphics.DrawLines(al .pen(),linelist);

}

catch

{

Drawdot = true; //我们想画一个点

}

if(Drawdot)

{

//画一个单点因为用户只点击了

,而不是点击并拖动

// TODO:画一个点

}


}

}

catch

{

error = true;

}

if(错误)

{

//this.OnPaint(e);

}

}

}


}


我会喜欢能够创建一个位图,用于绘制

OnPaint中的内容,以便我可以将其保存到文件中,我已经尝试了一些东西,但是所以

远远不及黑色位图的大小与

代码创建的控件大小相同,任何建议或代码片段都会有很大的帮助。


干杯


Lee

I have the following code
using System;
using System.Windows.Forms;
using System.Drawing;
using System.Collections;
namespace FINN
{

public class flickerFreePictureBox : PictureBox
{
public ArrayList Lines = new ArrayList();
public flickerFreePictureBox()
{

this.SetStyle(ControlStyles.DoubleBuffer, true);
this.SetStyle (ControlStyles.AllPaintingInWmPaint, true);
this.SetStyle(ControlStyles.UserPaint, true);
this.SetStyle(ControlStyles.ResizeRedraw, true);
//this.MouseMove += new MouseEventHandler(_MouseMove);
}
protected override void OnPaint(PaintEventArgs e)
{
bool error = false;
//e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
try
{
foreach(scribbleObject al in Lines)
{
bool Drawdot = false;
try
{
Point[] linelist = new Point[
al.linePositions.Count];

al.linePositions.CopyTo(0,linelist,0,al.linePositi ons.Count);
e.Graphics.DrawLines(al.pen(), linelist);
}
catch
{
Drawdot = true; //we want to draw a dot
}
if(Drawdot)
{
//Draw a single dot as the user has only
clicked, rather than clicked and dragged
//TODO: Draw a dot
}

}
}
catch
{
error = true;
}
if(error)
{
//this.OnPaint(e);
}
}
}

}

And I would like to be able to create a bitmap of what gets drawn in
OnPaint so that I can save it to file, I''ve tried a few things, but so
far just ended up with black bitmaps the same size as the control that
code creates, any suggestions or code snippets would be a great help.

Cheers

Lee

推荐答案

你好李


在鲍勃鲍威尔注意到你正在使用PictureBox进行绘图之前,你需要支付
sh我应该阅读GDI +常见问题解答。特别关于PictureBox的文章

http://www.bobpowell .net / pictureboxhowto.htm


至于保存位图你可以在你的Paint程序中分开

实际绘图到你通过的方法要么是Paint图形对象,要么是
或者是Bitmap图形对象,否则请参考常见问题


-

快乐编码!

Morten Wennevik [C#MVP]
Hi Lee

Before Bob Powell notices you are using a PictureBox for drawing, you
should read the GDI+ FAQ. Specifically the article regarding PictureBox

http://www.bobpowell.net/pictureboxhowto.htm

As for saving the Bitmap you could inside your Paint procedure separate
actual drawing to a method that you pass either the Paint graphics object,
or a Bitmap graphics object, otherwise refer to the FAQ

--
Happy Coding!
Morten Wennevik [C# MVP]


我做了以下


在OnPaint中添加了这个


位图bmp =新位图(this.Width,this.Height);

图形g = Graphics.FromImage(bmp);


那么对于我的绘画方法,只需复制它,以便它也可以画到

g。

现在我得到一个漂亮的黑色位图和不是很多:(


感谢您对常见问题解答的链接,但我不知道如何重新定义我的

当前使用它的代码,由于截止日期我认为它可能有等待下一个项目的



干杯


Morten Wennevik写道:
I''ve done the following

In OnPaint added this

Bitmap bmp = new Bitmap(this.Width, this.Height);
Graphics g = Graphics.FromImage(bmp);

Then for my painting method, simply duplicated it so that it paints to
g as well.
Now I get a nice black bitmap and not a lot else :(

Thanks for the link to the FAQ but I''m not sure how I can refactor my
current code to make use of it, due to deadlines I think it might have
to wait for the next project.

Cheers

Morten Wennevik wrote:

你好李


在鲍勃鲍威尔注意到你正在使用PictureBox进行绘图之前,你需要阅读GDI +常见问题解答。特别关于PictureBox的文章

http://www.bobpowell .net / pictureboxhowto.htm


至于保存位图你可以在你的Paint程序中分开

实际绘图到你通过的方法要么是Paint图形对象,要么是
或者是Bitmap图形对象,否则请参考常见问题


-

快乐编码!

Morten Wennevik [C#MVP]
Hi Lee

Before Bob Powell notices you are using a PictureBox for drawing, you
should read the GDI+ FAQ. Specifically the article regarding PictureBox

http://www.bobpowell.net/pictureboxhowto.htm

As for saving the Bitmap you could inside your Paint procedure separate
actual drawing to a method that you pass either the Paint graphics object,
or a Bitmap graphics object, otherwise refer to the FAQ

--
Happy Coding!
Morten Wennevik [C# MVP]


图形g = Graphics.FromImage(bmp);

以上行很好。

因此你是否用THAT图形对象做了所有绘图?

然后在绘图结束后保存位图(bmp)到磁盘?


(我记得)应该这样做。


Simon Tamman


" Lee" < lv ****** @ gmail.com写信息

新闻:11 ********************** @ b28g2000cwb .googlegr oups.com ...
Graphics g = Graphics.FromImage(bmp);

The above line is good.
Therefore do you then do all of the drawing with THAT graphics object?
Then after the drawing has finished save the bitmap (bmp) to disk?

That (from what I recall) should do the trick.

Simon Tamman

"Lee" <lv******@gmail.comwrote in message
news:11**********************@b28g2000cwb.googlegr oups.com...

我已完成以下工作


在OnPaint中添加了此


位图bmp =新位图(this.Width,this.Height);

图形g = Graphics.FromImage(bmp);


那么对于我的绘画方法,只需复制它,以便它也可以画到

g。

现在我得到一个漂亮的黑色位图而不是其他很多: (


感谢您提供常见问题解答的链接,但我不知道如何重构我的

当前代码以利用它,由于截止日期我认为等待下一个项目可能需要



干杯


Morten Wennevik写道:
I''ve done the following

In OnPaint added this

Bitmap bmp = new Bitmap(this.Width, this.Height);
Graphics g = Graphics.FromImage(bmp);

Then for my painting method, simply duplicated it so that it paints to
g as well.
Now I get a nice black bitmap and not a lot else :(

Thanks for the link to the FAQ but I''m not sure how I can refactor my
current code to make use of it, due to deadlines I think it might have
to wait for the next project.

Cheers

Morten Wennevik wrote:

你好李


在鲍勃鲍威尔注意到你正在使用PictureBox进行绘图之前,你需要

应该阅读GDI +常见问题解答。特别是有关PictureBox的文章

http://www.bobpowell.net /pictureboxhowto.htm


至于保存位图你可以在你的Paint程序中单独

实际绘图到你通过的方法油漆图形
Hi Lee

Before Bob Powell notices you are using a PictureBox for drawing, you
should read the GDI+ FAQ. Specifically the article regarding PictureBox

http://www.bobpowell.net/pictureboxhowto.htm

As for saving the Bitmap you could inside your Paint procedure separate
actual drawing to a method that you pass either the Paint graphics



对象,

object,


或a位图图形对象,否则请参考常见问题


-

快乐编码!

Morten Wennevik [C#MVP]
or a Bitmap graphics object, otherwise refer to the FAQ

--
Happy Coding!
Morten Wennevik [C# MVP]



这篇关于如何保存到位图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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