Resolved Using Media Player on Windows Form

MPIon

Well-known member
Joined
Jun 13, 2020
Messages
73
Location
England
Programming Experience
10+
I am trying to add a Media Player (or Windows Media Player) to my C# Form.
I have managed to find it (Windows Media Player) in COM components and added it to my application, but there was a message saying it was not enabled and did not show up in the ToolBox.

I have a feeling this is because I created the project as a Windows Forms App rather than a Windows Form App (.Net Framework).
This was confirmed by creating a new project as a Net Framework one, and the Windows Media Player was in the toolbox.

However, now I run into other problems in that some of the features that worked are not supported in the Net Framework one.
e.g. nullable reference types is not available in C# 7.3, Please use language version 8.0 or greater.
Also, it seems as if the Windows Form App (.Net Framework) is using Net 4.7 whereas the original Windows Forms App is using Net 6.

Sorry this is a bit convoluted, but it is difficult to get simple answers in plain English to explain all this.

I am a bit concerned that if I go to the Windows Form App (.Net Framework) I will be using a legacy system which does not support new language features.

So, basically my question is, do I have to use Windows Form App (.Net Framework) to be able to use Media Player, and if so, how do I create the project with Net 5 or 6 and the language version 8 or 9 maybe?
 
If you target .NET Framework then you don't "create the project with Net 5 or 6" because they are not .NET Framework. .NET 5 and later are .NET Core. .NET Core 3.1 and .NET Framework 4.8 were being developed concurrently. That was the last version of .NET Framework and the next version of .NET Core was .NET 5. They dropped the "Core" because it was the only game in town so no need to distinguish it and they skipped version 4.x to avoid any further confusion. .NET 5 basically rolled some Framework-specific features into Core so there was really no need for the two any more.
 
If you right-click the Framework project and select Unload Project, you can then edit the project file directly. You can specify the language version yourself there, e.g. here's one I prepared earlier:
C#:
  <PropertyGroup>
    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
    <ProjectGuid>{86B97037-3A63-4406-89E1-920A1D782172}</ProjectGuid>
    <OutputType>Exe</OutputType>
    <RootNamespace>Test.Console._481</RootNamespace>
    <AssemblyName>Test.Console.481</AssemblyName>
    <TargetFrameworkVersion>v4.8.1</TargetFrameworkVersion>
    <LangVersion>9</LangVersion>
    <FileAlignment>512</FileAlignment>
    <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
    <Deterministic>true</Deterministic>
    <NuGetPackageImportStamp>
    </NuGetPackageImportStamp>
  </PropertyGroup>
 
If you right-click the Framework project and select Unload Project, you can then edit the project file directly. You can specify the language version yourself there, e.g. here's one I prepared earlier:
C#:
  <PropertyGroup>
    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
    <ProjectGuid>{86B97037-3A63-4406-89E1-920A1D782172}</ProjectGuid>
    <OutputType>Exe</OutputType>
    <RootNamespace>Test.Console._481</RootNamespace>
    <AssemblyName>Test.Console.481</AssemblyName>
    <TargetFrameworkVersion>v4.8.1</TargetFrameworkVersion>
    <LangVersion>9</LangVersion>
    <FileAlignment>512</FileAlignment>
    <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
    <Deterministic>true</Deterministic>
    <NuGetPackageImportStamp>
    </NuGetPackageImportStamp>
  </PropertyGroup>

Great, thanks. I changed mine to Language Version 9 and the missing features are back.

From what I have read though, I still have to use Net Framework rather then Net 6 because not all Net Framework features (such as Media Player) are available in Net 6.
Also, when creating a new project, there was no option to use Net 6 for c# Desktop applications.
 
In general, the .NET (Core) set of apps were supposed to target apps to run multi-platform: Windows, Mac, Linux. Since there is no Windows Media Player or any other COM-style integration on the latter two, there was no compelling reason to make it available.

Originally, .NET (Core) was not supposed to support WinForms (and WPF support was going to be on for Windows). I think enough companies put pressure on Microsoft to continue to support WinForms, so Microsoft added WinForms in .NET Core 3.x but with the caveat of not a lot of investment was going to go into new feature development for it. So WinForms came in but a lot of the tooling around it didn't.
 
I am a bit concerned that if I go to the Windows Form App (.Net Framework) I will be using a legacy system which does not support new language features.

The irony of this of course being that you're concerned about using legacy language that doesn't allow modern c# features, yet you're definitely using a legacy media player!
 
The irony of this of course being that you're concerned about using legacy language that doesn't allow modern c# features, yet you're definitely using a legacy media player!

Yes, true. I did notice that Microsoft was suggesting using Media Player instead of Windows Media Player, but I could not find any information as to how to actually do that.
However, that is not a problem now, as I have removed the Windows Media Player from my application and just open up the default Media Player when a video file thumbnail is clicked.

I have now successfully managed to get thumbnails for all image or video files directly from the Thumbnail Cache. The only slight issue is I can't figure out how to get the Video sprockets on the thumbnail for videos. There is supposed to be an Adornment for this, which Windows Live Photo Gallery clearly uses.
 

No, I mean the sprockets down the side like so -
1692023261855.png


Refer to :-
Thumbnail Handlers (Windows)
 
What mode are you passing in to the API?

Thanks for the reply. Must admit that my knowledge is not yet that good that I understand the question.
To get the thumbnails, all I do is call shellFile.Thumbnail.LargeBitmap; for the appropriate image.

I see in the link you provided it has the lines :-
C#:
[Windows.Foundation.Metadata.Overload("GetThumbnailAsyncOverloadDefaultSizeDefaultOptions")]
[Windows.Foundation.Metadata.RemoteAsync]
public IAsyncOperation<StorageItemThumbnail> GetThumbnailAsync(ThumbnailMode mode);

When I add these to the code, I just get compiler errors. I'm afraid the link is just assuming too much knowledge that I don't have. It does not explain how you include the items in brackets.
I have noticed this with a lot of Microsoft documentation - its not much use to beginners (even though I have been coding in c# for 15+ years on and off).

I'll do some more research.
 
What mode are you passing in to the API?

This also only returns the thumbnail, without the adornments. In the Thumbnail Handlers article it said "Adornments are provided by Windows; do not create them yourself", I'm guessing this is something OS/Explorer adds to the views and never to the actual thumbnail.

To test the code create a .Net project (not .Net Framework). I created a Windows Forms .Net 7 project, and in Project Properties the Target OS Version and Supported version must be Windows 10.0.10240.0 or later. Then the Windows.Storage namespace is available:
C#:
var file = await StorageFile.GetFileFromPathAsync(filepath);
using var thumb = await file.GetThumbnailAsync(ThumbnailMode.VideosView, 256);
var ext = thumb.ContentType.Split('/')[1];
using var outfile = File.Create("test." + ext);
await thumb.AsStream().CopyToAsync(outfile);
 
This also only returns the thumbnail, without the adornments. In the Thumbnail Handlers article it said "Adornments are provided by Windows; do not create them yourself", I'm guessing this is something OS/Explorer adds to the views and never to the actual thumbnail.

To test the code create a .Net project (not .Net Framework). I created a Windows Forms .Net 7 project, and in Project Properties the Target OS Version and Supported version must be Windows 10.0.10240.0 or later. Then the Windows.Storage namespace is available:
C#:
var file = await StorageFile.GetFileFromPathAsync(filepath);
using var thumb = await file.GetThumbnailAsync(ThumbnailMode.VideosView, 256);
var ext = thumb.ContentType.Split('/')[1];
using var outfile = File.Create("test." + ext);
await thumb.AsStream().CopyToAsync(outfile);

Ok, great I'll give that a try in the next few days. If that works, it look likes I will have to convert my project back to Net 7 if Net Framework does not support this.
Am I right in thinking that Net 7 is the way to go now and Net Framework is a bit obsolete?
 
John, I tried this as you suggested and managed to get it to produce the thumbnail for a .wmv file, but it did not have video sprockets down the side, so maybe as you say it may be something that is added to the views not the thumbnails. So, I'm back to the drawing board for this unless someone knows how Explorer adds the sprockets.
Its not crucial, just a nice to have, so I may abandon this idea.
 
Read on Win32 tutorials on how to put icon overlays. The same concept applies where instead of an icon, you would have bitmap of the sprockets. You just apply the sprockets on the left and right sides of the thumbnail.
 

Latest posts

Back
Top Bottom