Click here to Skip to main content
15,889,403 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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?
Posted

You mean something like:
Bitmap to BitmapSource[^]
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 18-Feb-14 19:50pm    
My 5. My answer also shows this method, but the article explains it all in one place.
—SA
z3m 18-Feb-14 20:23pm    
No nothing like that, infect I am working on windows 8 store app and I came to know that System.Drawing.dll has been deprecated or some thing like that,
where as I was using a third party .dll and one of it's function was throwing a Bitmap and now I am unable to catch/proceed with that Bitmap....
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:
C#
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
 
Share this answer
 
Comments
Matt T Heffron 18-Feb-14 20:00pm    
+5.
The conversion is handy if there are images stored in .resx files, as they are exposed as System.Drawing.Bitmap
Sergey Alexandrovich Kryukov 18-Feb-14 20:18pm    
Exactly. This is one of the cases where such thing can be very reasonably used.
At the same time, a log of former bitmap graphics (not photographs), can be very conveniently migrated to vector graphics with WPF, using SVG to XAML.
—SA
z3m 18-Feb-14 20:03pm    
Problem here is that I want to do it for windows store app and development for store apps does not support System.Drwaing. infect that System.Drawing.dll has been deprecated. :(
Sergey Alexandrovich Kryukov 18-Feb-14 20:19pm    
Not so much of a problem. In principle, you still can use it, for bitmaps. Look at my answer again: in first line, I recommended you to thing about having only WPF images in first place. By the way, what forces you to use Store App? Just curious...
—SA
z3m 18-Feb-14 20:49pm    
@Sergey: university project forced me to us it.

1 more thing I have discovered System.Drawing.dll has been deprecated while I was using a third party api and a function of that api throws an System.Drawing.Bitmap I need to catch/save that bitmap but could not because that library has been eliminated :(

Exactly I want to split video file into multiple images.
Please see if you can help me with that.
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
 
Share this answer
 
v2
Comments
z3m 18-Feb-14 21:06pm    
I have used the same Aforge API.
I have used this code: VideoFileReader class is from the same library you have mentioned.
it's function ReadVideoFrame( ) returns me a Bitmap which caused all this hazard.

VideoFileReader reader = new VideoFileReader( );
// open video file
reader.Open( "test.avi" );
// check some of its attributes
Console.WriteLine( "width: " + reader.Width );
Console.WriteLine( "height: " + reader.Height );
Console.WriteLine( "fps: " + reader.FrameRate );
Console.WriteLine( "codec: " + reader.CodecName );
// read 100 video frames out of it
for ( int i = 0; i < 100; i++ )
{
Bitmap videoFrame = reader.ReadVideoFrame( );
// process the frame somehow
// ...

// dispose the frame when it is no longer required
videoFrame.Dispose( );
}
reader.Close( );
Sergey Alexandrovich Kryukov 18-Feb-14 21:11pm    
What is "the same"? Okay, very well, but did you solve your problem?
—SA
z3m 19-Feb-14 7:34am    
SA, I really don't have idea to work with these wrappers please help me, specially this C++ i am very bad in it.
Sergey Alexandrovich Kryukov 19-Feb-14 10:39am    
What C++? if you already use a wrapper, it's .NET, that's the purpose of this wrapper. I have no idea what help do you need?
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900