如何将System.Drawing转换为windows.graphics.imaging? [英] How do I convert System.Drawing into windows.graphics.imaging ?

查看:234
本文介绍了如何将System.Drawing转换为windows.graphics.imaging?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个返回System.Drawing.Bitmap的函数,我希望将其转换为windows.graphics.imaging格式,



任何人都可以帮我有了这个。因为我几乎尝试了所有可能的方法。有谁可以帮我这个?

解决方案

你的意思是:

Bitmap to BitmapSource [ ^ ]


我不确定你真的需要它,因为它会更好首先在WPF中完成所有操作。但是,有些情况下确实需要它。



第一种方法是:保存 System.Drawing.Bitmap 到内存流,然后从同一个流中读取 System.Windows.Media.Imaging.BitmapImage 。您可以在此处找到一些代码示例: http://stackoverflow.com/questions/2440267/interopbitmap-to-bitmapimage [ ^ ]。



我担心这个样本不是最优的。您可以以不同的方式执行此操作,而不使用两个不同的流:将第一个流回滚到开头,然后在WPF中再次使用相同的流 BitmapImage

 dImg.Save(ms,System.Drawing.Imaging.ImageFormat.Jpeg); 
ms.Seek( 0 ,SeekOrigin.Begin);
// ...





参见:

http://msdn.microsoft.com/en-us/library/system.io.memorystream.aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/system.io.memorystream.seek.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/883dhyx0.aspx [ ^ ]。



这种方法的问题在于它取决于位图持久呈现的细节,例如压缩。对于非压缩格式,可能需要太多的内存空间,否则压缩会影响图像质量。更好的方法是使用 System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap

http://msdn.microsoft.com/en-us/library/system.windows.interop.imaging .createbitmapsourcefromhbitmap%28v = vs.110%29.aspx [ ^ ]。



对于某些代码示例,请参阅: http://stackoverflow.com/questions/94456/load -a-wpf-bitmapimage-from-a-system-drawing-bitmap [ ^ ]。



-SA

< blockquote>

z3m写道:

我想把视频文件分成多个图像。请看看你是否可以帮助我。

我建​​议使用开源库或实用程序FFmpeg或libavcodec: http ://en.wikipedia.org/wiki/Ffmpeg [ ^ ],

http://ffmpeg.org/ [ ^ ],

http://en.wikipedia.org/wiki/Libavcodec [ ^ ],

http://libav.org/ [ ^ ]。



这是最好的图书馆和实用程序我知道的那一套。我做你需要的,还有更多的东西。我可以通过使用命令行界面以编程方式运行可用的实用程序(FFmpeg.exe)来使用。



可选,您可以嵌入为图书馆。使用C ++显然是可能的,因为库是用C编写的。如果你需要.NET功能,你也可以这样做:你仍然可以使用非托管库,在C ++ / CLI混合模式(托管+非托管)项目中使用它。您可以在托管的ref类中公开您需要的功能;然后你可以使用这些类作为通常的.NET程序集引用生成的模块。



使用.NET和没有C ++ / CLI,它可能更复杂,因为它需要使用P / Invoke。同时,您可以找到合适的包装器。请参阅:

http://www.ffmpeg-csharp.com/ [ ^ ],

http://sourceforge.net/projects/sharpffmpeg/ [ ^ ],

http://vbffmpegwrapper.codeplex.com/ [ ^ ]。



毕竟,尝试找一些: http://bit.ly/VpboUJ [ ^ ]。



如果你想工作在你自己的包装但不知道如何,问一个问题,我会给你基本的想法(使用P / Invoke或C ++ / CLI混合模式项目)。



祝你好运,

-SA


I have a function that returns a System.Drawing.Bitmap and I want to convert it into windows.graphics.imaging format,

Can any body help me with this. because I have tried almost every possible way I know. Any body who can help me with this?

解决方案

You mean something like:
Bitmap to BitmapSource[^]


I'm not sure you really needs that, as it would be better to do it all in WPF in first place. However, there are cases when it would be really needed.

First approach would be: save System.Drawing.Bitmap to memory stream, and then read System.Windows.Media.Imaging.BitmapImage from the same stream. You can find some code sample here: http://stackoverflow.com/questions/2440267/interopbitmap-to-bitmapimage[^].

I'm afraid this sample is not the most optimal. You can do it in a bit different way, without using two different streams: rewind the first stream to the beginning, and than use the same stream again in WPF BitmapImage:

dImg.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
ms.Seek(0, SeekOrigin.Begin);
//...



See also:
http://msdn.microsoft.com/en-us/library/system.io.memorystream.aspx[^],
http://msdn.microsoft.com/en-us/library/system.io.memorystream.seek.aspx[^],
http://msdn.microsoft.com/en-us/library/883dhyx0.aspx[^].

The problem with this approach is that it depends on detail of persistent presentation of bitmap, such as compression. It may need too much memory space for non-compressed formats, or else compression can compromise image quality. The better approach is using System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap
http://msdn.microsoft.com/en-us/library/system.windows.interop.imaging.createbitmapsourcefromhbitmap%28v=vs.110%29.aspx[^].

For some code sample, please see: http://stackoverflow.com/questions/94456/load-a-wpf-bitmapimage-from-a-system-drawing-bitmap[^].

—SA


z3m wrote:

Exactly I want to split video file into multiple images. Please see if you can help me with that.

I would advise to use open-source library or utility FFmpeg or libavcodec: http://en.wikipedia.org/wiki/Ffmpeg[^],
http://ffmpeg.org/[^],
http://en.wikipedia.org/wiki/Libavcodec[^],
http://libav.org/[^].

This is the best library and utility set I ever knew. I does what you need, plus a lot more things. I could be used just by running the available utility (FFmpeg.exe), programmatically or not, using command-line interface.

Optionally, you can embed is as a library. It's apparently possible with C++, as the library is written on C. Same thing if you need .NET functionality: you can still use the unmanaged library, using it in your C++/CLI mixed-mode (managed + unmanaged) project. You can expose just the functionality you need, in managed "ref" classes; then you can use these classes referencing the resulting module as a usual .NET assembly.

With .NET and without C++/CLI, it can be more complex as it would require using P/Invoke. At the same time, you can find appropriate wrapper. Please see:
http://www.ffmpeg-csharp.com/[^],
http://sourceforge.net/projects/sharpffmpeg/[^],
http://vbffmpegwrapper.codeplex.com/[^].

After all, try to find some more: http://bit.ly/VpboUJ[^].

If you wish to work at such wrapper by yourself but don't know how, ask a question, I'll give you the basic ideas (using P/Invoke or C++/CLI "mixed-mode" project).

Good luck,

—SA


这篇关于如何将System.Drawing转换为windows.graphics.imaging?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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