Video Thumbnail generation - c#

i am currently exploring on how to create thumbnail from video file. so far, i am able to generate thumbnail using ffmpeg but can somebody suggest me if there any other ways to generate thumbnail without involving executable(.exe) file.
Thanks,
New Learner, Please guide me...

ffmpeg.exe is the best solution for Video Thumbnails generation as it is free software licensed under the LGPL or GPL.
There are few other places where you will find the solution:
C# Wrapper for the AviFile Library
DirectShow .NET
SlimDX is a free open source framework that enables developers to easily build DirectX applications using .NET technologies such as C#

If you just need a generic thumbnail an easy way to get one is through the WindowsAPICodePack.Shell package. A small, medium, large, or extra large thumbnail can be generated. There's not as much control over the output as ffmpeg but it's hard to beat this method when a generic thumbnail will do or when avoiding an outside exe.
using Microsoft.WindowsAPICodePack.Shell;
using System.Drawing;
namespace GetVideoThumbnail
{
class VideoThumbnail
{
static void Main(string[] args)
{
string videoPath = #"C:\Users\NotSure\Desktop\SampleVideo.MP4";
// Create shell object
ShellObject videoShellObject = ShellObject.FromParsingName(videoPath);
// Create bitmap and/or save locally
Bitmap thumbnailBmp = videoShellObject.Thumbnail.LargeBitmap;
thumbnailBmp.Save(#"C:\Users\NotSure\Desktop\SampleVideoThumbnail.bmp");
}
}
}

Yea!,good this interestingly Q.
I worked it issue,my diploma tasking-at Uni.
And more yet Articles.
1.OpenCV - This Open source project Best worked C/C++,C# and Java
2.Emg CV - This good worked C#
, and more...
Try it's open sources.

You may want to try the VideoFileReader from AForge, specifically AForge.Video.FFMPEG.VideoFileReader. It makes use of ffmpeg DLLs.
Alternatively, on Windows 7, you could use ShellFile.Thumbnail from WindowsAPICodePack or use DirectShow to save a frame with VideoRenderer.
I recently used the ShellFile method, with VideoFileReader as backup.

Related

Retrieve some part of video without using software. Is it possible?

I am working on a project which is related to videos.
I need to cut part of a video (I want to retrieve that part of video which lies from 00:30:00 to 00:40:00).
I have searched about it and found it can be done by using ffmpeg (This is a command line tool which is used to edit and convert videos.) But I don't want to use any tool.
Is it possible to do this with code, rather then with another tool?
If what you actually want is to build the capability to cut samples out of existing video material into a .net program the splicer project might be what you're looking for.

how to convert sequence of jpeg image into video format?

hi
I am developing a video capture application using C#.net. i captured
video through webcam and saved it as a JPEG images then i want to make a
wmv file with those images. how can i do that what are the basic steps needed for that can any body help
I am working on this myself. I have found two ways that may be possible - both require the purchase of an outside library.
The first one looks to be the easiest but costs the most, although it will allow you to use it for free you will just have to deal with a pop up telling you to purchase the library: http://bytescout.com/products/developer/imagetovideosdk/imagetovideosdk_convert_jpg_to_video.html
The other involves using Microsoft Encoder 4. I am still working on this one. You can get the free version here: http://www.microsoft.com/download/en/details.aspx?id=18974
C# doesn't natively support much in the way of sound or video so outside reference assemblies seem to be a necessity.
Right now that is the best help I can offer until I figure it out.

JPEG 2000 support in C#.NET

It seems that .NET can't open JP2 (Jpeg 2000) files using the GDI library. I've searched on google but can't find any libraries or example code to do this.
Anybody got any ideas? I don't really want to pay for a library to do it unless I have to..
Seems like we can do it using FreeImage (which is free)
FIBITMAP dib = FreeImage.LoadEx("test.jp2");
//save the image out to disk
FreeImage.Save(FREE_IMAGE_FORMAT.FIF_JPEG, dib, "test.jpg", FREE_IMAGE_SAVE_FLAGS.JPEG_QUALITYNORMAL);
//or even turn it into a normal Bitmap for later use
Bitmap bitmap = FreeImage.GetBitmap(dib);
I was looking for something similar a while back, with a view to implementing one if I could; The responses to my question imply that there is no documented method to do this for GDI+ which the Image class in .Net uses.
I believe that if you're writing a WPF application, then you can extend the list of supported image formats through Windows Imanging Components codecs, and there may be one out there already (ask your local friendly search engine?)
There is an option to use an addon such as DotImage which supports JPEG2000, although there may be more "effort" involved in loading images.
For anyone coming across this old post, the above code from Gordon works great, but as jixtra pointed out, you will indeed get an exception: System.DllNotFoundException: 'Unable to load DLL 'FreeImage': The specified module could not be found.' when installing via nuget. I was able to get it working in .net 4.6.1 by installing the FreeImage-dotnet-core nuget package and manually adding the FreeImage.dll to the bin folder. You can download the dll here: http://freeimage.sourceforge.net/download.html.
I needed a better quality image to use with tesseract so I made a few minor changes which made a huge difference to the quality of the new jpeg:
var jp2Format = FREE_IMAGE_FORMAT.FIF_JP2;
var dib = FreeImage.LoadEx("test.jp2", ref jp2Format);
FreeImage.SetResolutionX(dib, 300);
FreeImage.SetResolutionY(dib, 300);
FreeImage.Save(FREE_IMAGE_FORMAT.FIF_JPEG, dib, "test.jpg", FREE_IMAGE_SAVE_FLAGS.JPEG_QUALITYSUPERB);
// NOTE: memory needs to be explicitly freed (GC won't do this)
FreeImage.UnloadEx(ref dib);
I've used Leadtools to display JPEG 2000 images. They provide a .NET library including WPF and WinForms controls to display the images. However, there is a reasonably steep price tag.
You can use Jpeg2000.Net library if you need a fully managed solution without unsafe blocks. Disclaimer: I am working on this library, the library is commercial.
Here is the basic sample for decoding of JPEG 2000 image to TIFF:
string fileName = ...; // path to JPEG 2000 image
using (var image = new J2kImage(fileName))
{
var options = new J2kDecodingOptions
{
UpsampleComponents = true
};
// Alternatively, you can decode only part of the image using J2kImage.DecodeArea method
var imageData = image.Decode(options);
imageData.Save(tiffFileName, J2kOutputFormat.Tiff);
}
Also, for a currently-up-to-date, open-source option you can use Emgu CV, which is a wrapper around OpenCV. Basic example code in C# looks like:
Mat image = CvInvoke.Imread(#"\Path\To\File.jp2");

How to render pdfs using C#

I want to load and draw pdf files graphically using C#. I don't need to edit them or anything, just render them at a given zoom level.
The pdf libraries I have found seem to be focussed on generation. How do I do this?
Thanks.
Google has open sourced its excellent PDF rendering engine - PDFium - that it wrote with Foxit Software.
There is a C# nuget package called PdfiumViewer which gives a C# wrapper around PDFium and allows PDFs to be displayed and printed.
I have used it and was very impressed with the quality of the rendering.
PDFium works directly with streams so it doesn't require any data to be written to disk.
This is my example from a WinForms app
public void LoadPdf(byte[] pdfBytes)
{
var stream = new MemoryStream(pdfBytes);
LoadPdf(stream);
}
public void LoadPdf(Stream stream)
{
// Create PDF Document
var pdfDocument = PdfDocument.Load(stream);
// Load PDF Document into WinForms Control
pdfRenderer.Load(pdfDocument);
}
Edit: To get the pdfRenderer control in WinForm: Add the PdfiumViewer NuGet package to the project; open the projects packages folder in Windows Explorer and drag the PdfiumViewer.dll file onto the Toolbox window; A control called PdfRenderer will be available to add:
There are a few other choices in case the Adobe ActiveX isn't what you're looking for (since Acrobat must be present on the user machine and you can't ship it yourself).
For creating the PDF preview, first have a look at some other discussions on the subject on StackOverflow:
How can I take preview of documents?
Get a preview jpeg of a pdf on windows?
.NET open PDF in winform without external dependencies
PDF Previewing and viewing
In the last two I talk about a few things you can try:
You can get a commercial renderer (PDFViewForNet, PDFRasterizer.NET, ABCPDF, ActivePDF, XpdfRasterizer and others in the other answers...).
Most are fairly expensive though, especially if all you care about is making a simple preview/thumbnails.
In addition to Omar Shahine's code snippet, there is a CodeProject article that shows how to use the Adobe ActiveX, but it may be out of date, easily broken by new releases and its legality is murky (basically it's ok for internal use but you can't ship it and you can't use it on a server to produce images of PDF).
You could have a look at the source code for SumatraPDF, an OpenSource PDF viewer for windows.
There is also Poppler, a rendering engine that uses Xpdf as a rendering engine.
All of these are great but they will require a fair amount of commitment to make make them work and interface with .Net and they tend to be be distributed under the GPL.
You may want to consider using GhostScript as an interpreter because rendering pages is a fairly simple process.
The drawback is that you will need to either re-package it to install it with your app, or make it a pre-requisite (or at least a part of your install process).
It's not a big challenge, and it's certainly easier than having to massage the other rendering engines into cooperating with .Net.
I did a small project that you will find on the Developer Express forums as an attachment.
Be careful of the license requirements for GhostScript through.
If you can't leave with that then commercial software is probably your only choice.
Here is my answer from a different question.
First you need to reference the Adobe Reader ActiveX Control
Adobe Acrobat Browser Control Type Library 1.0
%programfiles&\Common Files\Adobe\Acrobat\ActiveX\AcroPDF.dll
Then you just drag it into your Windows Form from the Toolbox.
And use some code like this to initialize the ActiveX Control.
private void InitializeAdobe(string filePath)
{
try
{
this.axAcroPDF1.LoadFile(filePath);
this.axAcroPDF1.src = filePath;
this.axAcroPDF1.setShowToolbar(false);
this.axAcroPDF1.setView("FitH");
this.axAcroPDF1.setLayoutMode("SinglePage");
this.axAcroPDF1.Show();
}
catch (Exception ex)
{
throw;
}
}
Make sure when your Form closes that you dispose of the ActiveX Control
this.axAcroPDF1.Dispose();
this.axAcroPDF1 = null;
otherwise Acrobat might be left lying around.
PdfiumViewer is great, but relatively tightly coupled to System.Drawingand WinForms. For this reason I created my own wrapper around PDFium: PDFiumSharp
Pages can be rendered to a PDFiumBitmap which in turn can be saved to disk or exposed as a stream. This way any framework capable of loading an image in BMP format from a stream can use this library to display pdf pages.
For example in a WPF application you could use the following method to render a pdf page:
using System.Linq;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using PDFiumSharp;
static class PdfRenderer
{
public static ImageSource RenderPage(string filename, int pageIndex, string password = null, bool withTransparency = true)
{
using (var doc = new PdfDocument(filename, password))
{
var page = doc.Pages[pageIndex];
using (var bitmap = new PDFiumBitmap((int)page.Width, (int)page.Height, withTransparency))
{
page.Render(bitmap);
return new BmpBitmapDecoder(bitmap.AsBmpStream(), BitmapCreateOptions.None, BitmapCacheOption.OnLoad).Frames.First();
}
}
}
}
ABCpdf will do this and many other things for you.
Not ony will it render your PDF to a variety of formats (eg JPEG, GIF, PNG, TIFF, JPEG 2000; vector EPS, SVG, Flash and PostScript) but it can also do so in a variety of color spaces (eg Gray, RGB, CMYK) and bit depths (eg 1, 8, 16 bits per component).
And that's just some of what it will do!
For more details see:
http://www.websupergoo.com/abcpdf-8.htm
Oh and you can get free licenses via the free license scheme.
There are EULA issues with using Acrobat to do PDF rendering. If you want to go down this route check the legalities very carefully first.
Use the web browser control. This requires Adobe reader to be installed but most likely you have it anyway. Set the UrL of the control to the file location.
You can add a NuGet package CefSharp.WinForms to your application and then add a ChromiumWebBroweser control to your form.
In the code you can write:
chromiumWebBrowser1.Load(filePath);
This is the easiest solution I have found, it is completely free and independent of the user's computer settings like it would be when using default WebBrowser control.
I would like to mention Docotic.Pdf library here. It can convert PDF to image in C# and VB.NET. And it can also render and print PDF documents.
The library is 100% managed without external dependencies. It does not depend on System.Drawing.dll or GDI+. You will get consistent output on Windows, Linux, macOS, iOS, and Android.
I am one of Docotic.Pdf developers, so I really like it. Please try it and you will probably like it, too.
You can also just return a byte[] of a pdf as a file. Here is an example of returning a rendered pdf in a C# web application from a local pdf file. Ideally the pdf would be stored as a byte[] in the database.
public async Task<ActionResult> ViewDocument(GetPdf pdfdata)
{
MemoryStream ms = new MemoryStream();
FileStream stream = new FileStream("Graph.pdf", FileMode.Open, FileAccess.Read);
stream.CopyTo(ms);
return File(ms.ToArray(), "application/pdf");
}
Dynamic PDF Viewer from ceTe software might do what you're looking for. I've used their generator software and was pretty happy with it.
http://www.dynamicpdf.com/
The easiest lib I have used is Paolo Gios's library. It's basically
Create GiosPDFDocument object
Create TextArea object
Add text, images, etc to TextArea object
Add TextArea object to PDFDocument object
Write to stream
This is a great tutorial to get you started.
This looks like the right thing: http://code.google.com/p/lib-pdf/
You could google for PDF viewer component, and come up with more than a few hits.
If you don't really need to embed them in your app, though - you can just require Acrobat Reader or FoxIt (or bundle it, if it meets their respective licensing terms) and shell out to it. It's not as cool, but it gets the job done for free.

OCR with the Tesseract interface

How do you OCR an tiff file using Tesseract's interface in c#?
Currently I only know how to do it using the executable.
Take a look at tessnet
The source code seemed to be geared for an executable, you might need to rewire stuffs a bit so it would build as a DLL instead. I don't have much experience with Visual C++ but I think it shouldn't be too hard with some research. My guess is that someone might have had made a library version already, you should try Google.
Once you have tesseract-ocr code in a DLL file, you can then import the file into your C# project via Visual Studio and have it create wrapper classes and do all the marshaling stuffs for you. If you can't import then DllImport will let you call the functions in the DLL from C# code.
Then you can take a look at the original executable to find clues on what functions to call to properly OCR a tiff image.
C# program launches tesseract.exe and then reads the output file of tesseract.exe.
Process process = Process.Start("tesseract.exe", "out");
process.WaitForExit();
if (process.ExitCode == 0)
{
string content = File.ReadAllText("out.txt");
}
I discovered today that EMGU now includes a Tesseract wrapper. While the number of unmanaged dlls of the opencv lib might seem a little daunting, it's nothing that a quick copy to your output directory won't cure. From there the actual OCR process is as simple as three lines:
Tesseract ocr = new Tesseract(Path.Combine(Environment.CurrentDirectory, "tessdata"), "eng", Tesseract.OcrEngineMode.OEM_TESSERACT_ONLY);
this.ocr.Recognize(clip);
optOCR.Text = this.ocr.GetText();
"robomatics" put together a very nice youtube video that demonstrates a simple but effective solution.
Disclaimer: I work for Atalasoft
Our OCR module supports Tesseract and if that proves to not be good enough, you can upgrade to a better engine and just change one line of code (we provide a common interface to multiple OCR engines).

Categories