Creatng a thumb from a videofile, always get a black image file with MediaPlayer :(

V

Volkan Senguel

Hi i'm trying to create a thumb (image file) from a video file.
its a test winform application which uses the framework 3.5SP1.

here are my methods:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using System.Windows;
using System.Threading;
using System.IO;
using System.Windows.Media;
using System.Windows.Media.Imaging;

.....

public static void CreateThumbnail(string filePath, int width, int
height, int quality, ImageType imageType)
{
if (File.Exists(filePath))
{
string ext = Path.GetExtension(filePath).ToLower();
try
{
if (VALID_EXTENSIONS_MOVIES.Contains(ext))
{
MediaPlayer player = new MediaPlayer();
player.Open(new Uri(filePath, UriKind.Absolute));
player.ScrubbingEnabled = true;
player.Play();
player.Pause();
player.Position = new TimeSpan(0, 0, 2);

Thread.Sleep(4000);
RenderTargetBitmap rtb = new
RenderTargetBitmap(width, height, 1 / (double)200, 1 / (double)200,
PixelFormats.Pbgra32);
DrawingVisual dv = new DrawingVisual();
DrawingContext dc = dv.RenderOpen();
dc.DrawVideo(player, new Rect(0, 0, width, height));
dc.Close();
rtb.Render(dv);
BitmapEncoder encoder =
CreateBitmapEncoder(imageType, quality);
encoder.Frames.Add(BitmapFrame.Create(rtb));


// Write to file < PROBLEM ???
FileStream stream = new
FileStream(@"D:\Filme\Thumb.png", FileMode.Create);
encoder.Save(stream);
// End writing

player.Stop();
GC.Collect();
}
}
catch (Exception exp)
{
MessageBox.Show(exp.Message);
}
}
}

protected static BitmapEncoder CreateBitmapEncoder(ImageType
imageType, int quality)
{
BitmapEncoder result;
switch (imageType)
{
case ImageType.Png:
result = new PngBitmapEncoder();
break;
case ImageType.Jpeg:
result = new JpegBitmapEncoder();
((JpegBitmapEncoder)result).QualityLevel = quality;
break;
default:
throw new ArgumentException(string.Format("Thumbnail
type '{0}' not supported", imageType));
}
return result;
}

public enum ImageType
{
Unknown = 0,
Jpeg = 1,
Png = 2
}
how can i write a image to the file instead a black one?

thank you for any advice
Volkan Senguel
 
V

Volkan Senguel

No one a tip?

Volkan Senguel said:
Hi i'm trying to create a thumb (image file) from a video file.
its a test winform application which uses the framework 3.5SP1.

here are my methods:


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using System.Windows;
using System.Threading;
using System.IO;
using System.Windows.Media;
using System.Windows.Media.Imaging;

....

public static void CreateThumbnail(string filePath, int width, int
height, int quality, ImageType imageType)
{
if (File.Exists(filePath))
{
string ext = Path.GetExtension(filePath).ToLower();
try
{
if (VALID_EXTENSIONS_MOVIES.Contains(ext))
{
MediaPlayer player = new MediaPlayer();
player.Open(new Uri(filePath, UriKind.Absolute));
player.ScrubbingEnabled = true;
player.Play();
player.Pause();
player.Position = new TimeSpan(0, 0, 2);

Thread.Sleep(4000);
RenderTargetBitmap rtb = new
RenderTargetBitmap(width, height, 1 / (double)200, 1 / (double)200,
PixelFormats.Pbgra32);
DrawingVisual dv = new DrawingVisual();
DrawingContext dc = dv.RenderOpen();
dc.DrawVideo(player, new Rect(0, 0, width,
height));
dc.Close();
rtb.Render(dv);
BitmapEncoder encoder =
CreateBitmapEncoder(imageType, quality);
encoder.Frames.Add(BitmapFrame.Create(rtb));


// Write to file < PROBLEM ???
FileStream stream = new
FileStream(@"D:\Filme\Thumb.png", FileMode.Create);
encoder.Save(stream);
// End writing

player.Stop();
GC.Collect();
}
}
catch (Exception exp)
{
MessageBox.Show(exp.Message);
}
}
}

protected static BitmapEncoder CreateBitmapEncoder(ImageType
imageType, int quality)
{
BitmapEncoder result;
switch (imageType)
{
case ImageType.Png:
result = new PngBitmapEncoder();
break;
case ImageType.Jpeg:
result = new JpegBitmapEncoder();
((JpegBitmapEncoder)result).QualityLevel = quality;
break;
default:
throw new ArgumentException(string.Format("Thumbnail
type '{0}' not supported", imageType));
}
return result;
}

public enum ImageType
{
Unknown = 0,
Jpeg = 1,
Png = 2
}

how can i write a image to the file instead a black one?

thank you for any advice
Volkan Senguel
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top