问题描述
本人想实现视频转缩略图,如果media中的视频多的话,media/thumb文件夹下生成的缩略图是黑色的,怎么解决?还有怎么提速。。。。MainWindow.xaml.cs的代码:usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Windows;usingSystem.Windows.Controls;usingSystem.Windows.Data;usingSystem.Windows.Documents;usingSystem.Windows.Input;usingSystem.Windows.Media;usingSystem.Windows.Media.Imaging;usingSystem.Windows.Navigation;usingSystem.Windows.Shapes;usingSystem.IO;usingSystem.Runtime.InteropServices;usingSystem.Windows.Threading;usingSystem.Windows.Controls.Primitives;namespacevideo{///<summary>///MainWindow.xaml的交互逻辑///</summary>publicpartialclassMainWindow:Window{publicstringMediaDir;publicstringMediaThumbDir;publicstringMediaThumbDirs;publicintwait=0;publicMainWindow(){InitializeComponent();load_data();}privatevoidload_data(){MediaDir=System.IO.Path.GetDirectoryName(System.Windows.Application.ResourceAssembly.Location)+"\media";MediaThumbDir=MediaDir+"\thumb";if(!Directory.Exists(MediaDir)){Directory.CreateDirectory(MediaDir);}//检测视频文件夹if(!Directory.Exists(MediaThumbDir)){Directory.CreateDirectory(MediaThumbDir);}//检测视频缩略图文件夹DirectoryInfodi=newDirectoryInfo(MediaDir);FileInfo[]listmedia=di.GetFiles();if(listmedia.Length>0){foreach(FileInfofinfoinlistmedia){stringTempStr1=System.IO.Path.GetExtension(finfo.FullName);if(TempStr1.ToLower()==".avi"||TempStr1.ToLower()==".wmv"||TempStr1.ToLower()==".3gp"){wait++;stringmediathumbname=System.IO.Path.GetFileNameWithoutExtension(finfo.FullName)+"-video";stringfilename=finfo.FullName;varfilename1=newUri(filename);//查看视频缩略图是否存在if(!File.Exists(MediaThumbDir+"\"+mediathumbname+".jpg")){VideoScreenShot.CaptureScreenAsync(filename1,newDictionary<TimeSpan,object>{{TimeSpan.FromSeconds(1),mediathumbname},},.1,makeJpeg,makeThumbnails);}}}}}privatedelegatevoidsetImageDelegate(stringcontrolName,BitmapFrameframe);privatevoidsetImage(stringcontrolName,BitmapFrameframe){varcontrol=FindName(controlName);if(control!=null)((Image)control).Source=frame;}privatevoidmakeThumbnails(BitmapFrameframe,objectstate){Dispatcher.Invoke(newsetImageDelegate(setImage),(string)state,frame);}privatevoidmakeJpeg(BitmapFrameframe,objectstate){varencoder=newJpegBitmapEncoder();encoder.Frames.Add(frame);stringMediaThumbDirs=MediaThumbDir+"\";stringfilename=MediaThumbDirs+(string)state+".jpg";using(varfs=newFileStream(filename,FileMode.Create))encoder.Save(fs);}}}VideoScreenShot.cs的代码usingSystem;usingSystem.Collections.Generic;usingSystem.Threading;usingSystem.Windows;usingSystem.Windows.Media;usingSystem.Windows.Media.Imaging;namespacevideo{publicclassVideoScreenShot{publicintaa;publicintbb;publicdelegatevoidCaptureWorkerDelegate(BitmapFrameframe,objectstate);publicstaticvoidCaptureScreenAsync(Urisource,Dictionary<TimeSpan,object>captureList,doublescale,CaptureWorkerDelegatefinalWorkerPrimary,CaptureWorkerDelegatefinalWorkerThumbnail){ThreadPool.QueueUserWorkItem(delegate{CaptureScreen(source,captureList,scale,finalWorkerPrimary,finalWorkerThumbnail);});}publicstaticvoidCaptureScreen(Urisource,Dictionary<TimeSpan,object>captureList,doublescale,CaptureWorkerDelegatefinalWorkerPrimary,CaptureWorkerDelegatefinalWorkerThumbnail){varmutexLock=newMutex(false,source.GetHashCode().ToString());mutexLock.WaitOne();varplayer=newMediaPlayer{Volume=0,ScrubbingEnabled=true};player.Open(source);player.Pause();//intwaits=0;foreach(varpairincaptureList){//waits++;vartimeSpan=pair.Key;varstate=pair.Value;player.Position=timeSpan;//Thread.Sleep(2000+waits*2000);Thread.Sleep(3000);intwidth=player.NaturalVideoWidth;if(width<=0){width=640;}intheight=player.NaturalVideoHeight;if(height<=0){height=480;}varrtb=newRenderTargetBitmap(width,height,96,96,PixelFormats.Pbgra32);vardv=newDrawingVisual();using(DrawingContextdc=dv.RenderOpen())dc.DrawVideo(player,newRect(0,0,width,height));rtb.Render(dv);varframe=BitmapFrame.Create(rtb).GetCurrentValueAsFrozen();if(finalWorkerPrimary!=null)finalWorkerPrimary(frameasBitmapFrame,state);if(scale>0&&finalWorkerThumbnail!=null){varthumbnailFrame=BitmapFrame.Create(newTransformedBitmap(frameasBitmapSource,newScaleTransform(scale,scale))).GetCurrentValueAsFrozen();varencoder=newJpegBitmapEncoder();encoder.Frames.Add(thumbnailFrameasBitmapFrame);finalWorkerThumbnail(thumbnailFrameasBitmapFrame,state);}//Thread.Sleep(3000);}player.Close();mutexLock.ReleaseMutex();}}}
解决方案
本帖最后由 sxliusir 于 2011-06-03 14:34:13 编辑
解决方案二:
Graphicsg=Graphics.FromImage(image);g.Clear(Color.White);