Skip to content

Commit

Permalink
Modify image loading strategy & Resolve memory leaks
Browse files Browse the repository at this point in the history
  • Loading branch information
xuan25 committed Jul 13, 2019
1 parent e00e489 commit 7932489
Show file tree
Hide file tree
Showing 37 changed files with 156 additions and 335 deletions.
55 changes: 1 addition & 54 deletions Bili-dl/Bili-dl/BiliApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media.Imaging;

namespace Bili
{
Expand All @@ -35,7 +33,7 @@ class BiliApi
/// <returns>Parameter string</returns>
public static string DicToParams(Dictionary<string, string> dic, bool addVerification)
{
if(dic != null)
if (dic != null)
{
if (addVerification)
dic = AddVerification(dic);
Expand Down Expand Up @@ -161,57 +159,6 @@ public static Task<IJson> GetJsonResultAsync(string url, Dictionary<string, stri
return task;
}

/// <summary>
/// Download an Image asynchronously.
/// </summary>
/// <param name="url">Image url</param>
/// <returns>Image data</returns>
public static Task<System.Drawing.Bitmap> GetImageAsync(string url)
{
Task<System.Drawing.Bitmap> task = new Task<System.Drawing.Bitmap>(() =>
{
return GetImage(url);
});
task.Start();
return task;
}

/// <summary>
/// Download an Image.
/// </summary>
/// <param name="url">Image url</param>
/// <returns>Image data</returns>
public static System.Drawing.Bitmap GetImage(string url)
{
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream dataStream = response.GetResponseStream();
System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(dataStream);
response.Close();
dataStream.Close();
return bitmap;
}
catch (Exception)
{
return new System.Drawing.Bitmap(1,1);
}

}

/// <summary>
/// Convert a bitmap to an ImageSource.
/// </summary>
/// <param name="bitmap">Source bitmap</param>
/// <returns>ImageSource</returns>
public static BitmapSource BitmapToImageSource(System.Drawing.Bitmap bitmap)
{
IntPtr ip = bitmap.GetHbitmap();
BitmapSource bitmapSource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(ip, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
return bitmapSource;
}

/// <summary>
/// Formatting numbers.
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion Bili-dl/Bili-dl/BiliDownload/DownloadOption.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ private void PageListItem_Selected(object sender, RoutedEventArgs e)
private void ShowQualies(VideoInfo.Page page)
{
List<VideoInfo.Page.Quality> qualities = page.GetQualities();
if(qualities != null)
if (qualities != null)
Dispatcher.Invoke(new Action(() =>
{
foreach (VideoInfo.Page.Quality quality in qualities)
Expand Down
10 changes: 5 additions & 5 deletions Bili-dl/Bili-dl/BiliDownload/DownloadQueue.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ public DownloadQueue()
/// <returns>Successful</returns>
public bool Append(DownloadTask downloadTask)
{
foreach(ListBoxItem i in QueueList.Items)
foreach (ListBoxItem i in QueueList.Items)
{
DownloadQueueItem dqi = (DownloadQueueItem)i.Content;
if(dqi.downloadTask.Aid == downloadTask.Aid &&
if (dqi.downloadTask.Aid == downloadTask.Aid &&
dqi.downloadTask.Cid == downloadTask.Cid &&
dqi.downloadTask.Qn == downloadTask.Qn)
{
Expand Down Expand Up @@ -67,7 +67,7 @@ private void DownloadQueueItem_Finished(DownloadQueueItem downloadQueueItem)
{
QueueList.Items.Remove(ItemMap[downloadQueueItem]);
ItemMap.Remove(downloadQueueItem);
if(QueueList.Items.Count > 0)
if (QueueList.Items.Count > 0)
((DownloadQueueItem)((ListBoxItem)QueueList.Items[0]).Content).Start();
}));
ConfigManager.ConfigManager.RemoveDownloadInfo(downloadQueueItem.downloadTask.Info);
Expand All @@ -80,8 +80,8 @@ private void UserControl_MouseLeftButtonDown(object sender, MouseButtonEventArgs

public void StopAll()
{
foreach(ListBoxItem listBoxItem in QueueList.Items)
if(((DownloadQueueItem)listBoxItem.Content).downloadTask.IsRunning)
foreach (ListBoxItem listBoxItem in QueueList.Items)
if (((DownloadQueueItem)listBoxItem.Content).downloadTask.IsRunning)
((DownloadQueueItem)listBoxItem.Content).downloadTask.Stop();
}

Expand Down
2 changes: 1 addition & 1 deletion Bili-dl/Bili-dl/BiliDownload/DownloadQueueItem.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ private void DownloadTask_AnalysisFailed(DownloadTask downloadTask)
{

}

}

private void DownloadTask_StatusUpdate(double progressPercentage, long bps, DownloadTask.Status status)
Expand Down
16 changes: 8 additions & 8 deletions Bili-dl/Bili-dl/BiliDownload/DownloadTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ private bool Analysis()
try
{
IJson json = BiliApi.GetJsonResult("https://api.bilibili.com/x/player/playurl", dic, false);
if(json.GetValue("code").ToLong() == 0)
if (json.GetValue("code").ToLong() == 0)
if (json.GetValue("data").GetValue("quality").ToLong() == Qn)
foreach (IJson v in json.GetValue("data").GetValue("durl"))
{
Expand Down Expand Up @@ -110,7 +110,7 @@ private bool Analysis()

private void Segment_Finished()
{
if(CurrentSegment < Segments.Count-1)
if (CurrentSegment < Segments.Count - 1)
{
CurrentSegment++;
Segments[CurrentSegment].Download();
Expand Down Expand Up @@ -194,7 +194,7 @@ public void Stop()
runThread.Abort();
if (!IsFinished && IsRunning)
{
if(CurrentSegment != -1)
if (CurrentSegment != -1)
Segments[CurrentSegment].AbortDownload();
AbortProgressMonitor();
IsRunning = false;
Expand All @@ -212,7 +212,7 @@ public void Clean()
if (Segments != null)
foreach (Segment segment in Segments)
segment.Clean();
}
}
}

private void StartProgressMonitor()
Expand All @@ -234,7 +234,7 @@ private void AbortProgressMonitor()
private void ProgressMonitor()
{
long total = 0;
foreach(Segment segment in Segments)
foreach (Segment segment in Segments)
{
total += segment.Length;
}
Expand Down Expand Up @@ -298,7 +298,7 @@ public Segment(uint aid, string url, SegmentType segmentType, long contentLength
Filepath = string.Format("{0}{1}", directory, Url.Substring(Url.LastIndexOf('/') + 1, Url.IndexOf('?') - Url.LastIndexOf('/') - 1));
Threads = threads;
DownloadThreads = new List<DownloadThread>();
for (int i=0; i < threads; i++)
for (int i = 0; i < threads; i++)
{
DownloadThread downloadThread;
if (i != threads - 1)
Expand Down Expand Up @@ -338,7 +338,7 @@ public void Download()
foreach (DownloadThread downloadThread in DownloadThreads)
{
downloadThread.StartDownloadThread();
}
}
}

/// <summary>
Expand Down Expand Up @@ -370,7 +370,7 @@ public void Clean()
private void DownloadThread_Finished()
{
FinishedThreadCount++;
if(FinishedThreadCount == DownloadThreads.Count)
if (FinishedThreadCount == DownloadThreads.Count)
{
bool flag = true;
foreach (DownloadThread downloadThread in DownloadThreads)
Expand Down
2 changes: 1 addition & 1 deletion Bili-dl/Bili-dl/BiliDownload/Flv.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public static void Merge(List<string> inputs, string output)
{
FileStream outputStream = new FileStream(output, FileMode.Create);
int timeOffest = 0;
for(int i=0; i<inputs.Count; i++)
for (int i = 0; i < inputs.Count; i++)
{
FileStream inputStream = new FileStream(inputs[i], FileMode.Open);
timeOffest = Merge(inputStream, outputStream, timeOffest);
Expand Down
22 changes: 11 additions & 11 deletions Bili-dl/Bili-dl/BiliDownload/VideoInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public VideoInfo(IJson json, bool isSeason)
pages.Add(new Page(Title, Aid, p, isSeason));
}
}

}

/// <summary>
Expand All @@ -62,7 +62,7 @@ public static VideoInfo GetInfo(uint id, bool isSeason)
{
return null;
}

}
else
{
Expand All @@ -71,17 +71,17 @@ public static VideoInfo GetInfo(uint id, bool isSeason)
try
{
IJson json = BiliApi.GetJsonResult("https://bangumi.bilibili.com/view/web_api/season", dic, false);
if(json.GetValue("code").ToLong() == 0)
if (json.GetValue("code").ToLong() == 0)
return new VideoInfo(json.GetValue("result"), isSeason);
return null;
}
catch (System.Net.WebException)
{
return null;
}

}

}

/// <summary>
Expand Down Expand Up @@ -140,7 +140,7 @@ public Page(string title, uint aid, IJson json, bool isSeason)
Part = json.GetValue("index_title").ToString();
Duration = (uint)json.GetValue("duration").ToLong();
}

}

/// <summary>
Expand All @@ -158,8 +158,8 @@ public List<Quality> GetQualities()
IJson json = BiliApi.GetJsonResult("https://api.bilibili.com/x/player/playurl", dic, false);
Qualities = new List<Quality>();
if (json.GetValue("code").ToLong() == 0)
for (int i = 0; i < ((JsonArray)json.GetValue("data").GetValue("accept_quality")).Count; i++)
Qualities.Add(new Quality(Title, Index, Num, Part, Aid, Cid, (uint)json.GetValue("data").GetValue("accept_quality").GetValue(i).ToLong(), json.GetValue("data").GetValue("accept_description").GetValue(i).ToString(), false));
for (int i = 0; i < ((JsonArray)json.GetValue("data").GetValue("accept_quality")).Count; i++)
Qualities.Add(new Quality(Title, Index, Num, Part, Aid, Cid, (uint)json.GetValue("data").GetValue("accept_quality").GetValue(i).ToLong(), json.GetValue("data").GetValue("accept_description").GetValue(i).ToString(), false));
else if (IsSeason)
{
json = BiliApi.GetJsonResult("http://api.bilibili.com/pgc/player/web/playurl", dic, false);
Expand All @@ -175,7 +175,7 @@ public List<Quality> GetQualities()
{
return null;
}

}

/// <summary>
Expand Down Expand Up @@ -239,13 +239,13 @@ public Quality(string title, string index, uint num, string part, uint aid, uint
IJson json = BiliApi.GetJsonResult("http://api.bilibili.com/pgc/player/web/playurl", dic, false);
IsAvaliable = json.GetValue("result").GetValue("quality").ToLong() == Qn;
}

}
catch (System.Net.WebException)
{
IsAvaliable = false;
}

}
}
}
Expand Down
4 changes: 2 additions & 2 deletions Bili-dl/Bili-dl/BiliLogin/BiliLoginQR.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ class BiliLoginQR
public BiliLoginQR(Window parent)
{
isTimeout = false;
if(parent != null)
if (parent != null)
parent.Closed += Parent_Closed;
}

Expand Down Expand Up @@ -154,7 +154,7 @@ public bool Init()

private void LoginListener()
{
while(!Init())
while (!Init())
{
Thread.Sleep(5000);
}
Expand Down
12 changes: 8 additions & 4 deletions Bili-dl/Bili-dl/BiliLogin/MoblieLoginWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using Bili;
using System;
using System;
using System.Drawing;
using System.Net;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media.Imaging;

namespace BiliLogin
{
Expand Down Expand Up @@ -50,7 +50,7 @@ public partial class MoblieLoginWindow : Window
public MoblieLoginWindow(Window parent)
{
InitializeComponent();
if(parent != null)
if (parent != null)
parent.Closed += Parent_Closed;
}

Expand Down Expand Up @@ -94,11 +94,15 @@ public void RefreshQRCode()
biliLoginQR.Begin();
}

[System.Runtime.InteropServices.DllImport("gdi32")] static extern int DeleteObject(IntPtr o);
private void BiliLoginQR_QRImageLoaded(BiliLoginQR sender, Bitmap qrImage)
{
Dispatcher.Invoke(new Action(() =>
{
QrImageBox.Source = BiliApi.BitmapToImageSource(qrImage);
IntPtr hBitmapIntPtr = qrImage.GetHbitmap();
BitmapSource bitmapSource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(hBitmapIntPtr, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
DeleteObject(hBitmapIntPtr);
QrImageBox.Source = bitmapSource;
}));
}

Expand Down
9 changes: 6 additions & 3 deletions Bili-dl/Bili-dl/BiliLogin/QRCoder/AbstractQRCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,21 @@ public abstract class AbstractQRCode
{
protected QRCodeData QrCodeData { get; set; }

protected AbstractQRCode() {
protected AbstractQRCode()
{
}

protected AbstractQRCode(QRCodeData data) {
protected AbstractQRCode(QRCodeData data)
{
this.QrCodeData = data;
}

/// <summary>
/// Set a QRCodeData object that will be used to generate QR code. Used in COM Objects connections
/// </summary>
/// <param name="data">Need a QRCodeData object generated by QRCodeGenerator.CreateQrCode()</param>
virtual public void SetQRCodeData(QRCodeData data) {
virtual public void SetQRCodeData(QRCodeData data)
{
this.QrCodeData = data;
}

Expand Down
Loading

0 comments on commit 7932489

Please sign in to comment.