Please help... I am stuck at this for 2 weeks. Thank you.
Having these codes
public static Bitmap GetThumbnail(string video, string thumbnail)
{
var cmd = "ffmpeg -itsoffset -1 -i " + '"' + video + '"' + " -vcodec mjpeg -vframes 1 -an -f rawvideo -s 320x240 " + '"' + thumbnail + '"';
var startInfo = new ProcessStartInfo
{
WindowStyle = ProcessWindowStyle.Hidden,
FileName = "cmd.exe",
Arguments = "/C " + cmd
};
var process = new Process
{
StartInfo = startInfo
};
process.Start();
process.WaitForExit(5000);
return LoadImage(thumbnail);
}
static Bitmap LoadImage(string path)
{
var ms = new MemoryStream(File.ReadAllBytes(path));
return (Bitmap)Image.FromStream(ms);
}
How do I retrieve and display the thumbnail in aspx file?
I suppose that your code is working and get the Bitmap... in this case you create a handler lets names it image.ashx
.
You put inside your handler this code
public void ProcessRequest(HttpContext context)
{
// assume its jpeg
context.Response.ContentType = "image/jpeg";
// here I call your "working" code
var cImageToRender = GetThumbnail("VideoName", "thumbnail");
// convert it for the stream
var imgData = BitmapToByteArray(cImageToRender);
// send it to the browser
context.Response.OutputStream.Write(imgData, 0, imgData.Length);
}
// this function reference
// https://stackoverflow.com/a/17244814/159270
public static byte[] BitmapToByteArray(Bitmap bitmap)
{
BitmapData bmpdata = null;
try
{
bmpdata = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadOnly, bitmap.PixelFormat);
int numbytes = bmpdata.Stride * bitmap.Height;
byte[] bytedata = new byte[numbytes];
IntPtr ptr = bmpdata.Scan0;
Marshal.Copy(ptr, bytedata, 0, numbytes);
return bytedata;
}
finally
{
if (bmpdata != null)
bitmap.UnlockBits(bmpdata);
}
}
and inside your .aspx
(or any html) page you get the image as
<img src="image.ashx">
This is in general the steps to follow - small details maybe need to be fixed in the final code.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.