Skip to content
This repository was archived by the owner on May 21, 2022. It is now read-only.

Commit 14ea158

Browse files
committed
feat: finish get image part
1 parent 8de7389 commit 14ea158

File tree

1 file changed

+30
-35
lines changed

1 file changed

+30
-35
lines changed

md2docx/ImageGetter.cs

Lines changed: 30 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
25
using System.Drawing;
6+
using System.Threading.Tasks;
37
using System.IO;
48
using System.Net;
59

@@ -24,25 +28,16 @@ public bool Load(string origin)
2428
{
2529
bool isUrl = Uri.TryCreate(origin, UriKind.Absolute, out Uri uriResult)
2630
&& (uriResult.Scheme == Uri.UriSchemeHttp || uriResult.Scheme == Uri.UriSchemeHttps);
27-
if (isUrl)
28-
{
29-
GetImageFromUrl(origin);
30-
}
31-
else
32-
{
33-
GetImageFromPath(origin);
34-
}
35-
return false;
36-
}
37-
38-
private bool GetImageFromPath(string path)
39-
{
4031
try
4132
{
42-
Image img = Image.FromFile(path);
43-
ImageData = File.ReadAllBytes(path);
44-
Width = img.Width;
45-
Height = img.Height;
33+
if (isUrl)
34+
{
35+
GetImageFromUrl(origin);
36+
}
37+
else
38+
{
39+
GetImageFromPath(origin);
40+
}
4641
return true;
4742
}
4843
catch
@@ -52,30 +47,30 @@ private bool GetImageFromPath(string path)
5247
}
5348
}
5449

50+
private bool GetImageFromPath(string path)
51+
{
52+
Image img = Image.FromFile(path);
53+
ImageData = File.ReadAllBytes(path);
54+
Width = img.Width;
55+
Height = img.Height;
56+
return true;
57+
}
58+
5559
private bool GetImageFromUrl(string url)
5660
{
57-
Console.WriteLine("WTF");
58-
try
61+
using (WebClient webClient = new WebClient())
5962
{
60-
using (WebClient webClient = new WebClient())
61-
{
62-
byte[] data = webClient.DownloadData(url);
63+
byte[] data = webClient.DownloadData(url);
6364

64-
using (MemoryStream mem = new MemoryStream(data))
65-
{
66-
Image img = Image.FromStream(mem);
67-
Width = img.Width;
68-
Height = img.Height;
69-
data.CopyTo(ImageData, 0);
70-
}
65+
using (MemoryStream mem = new MemoryStream(data))
66+
{
67+
Image img = Image.FromStream(mem);
68+
Width = img.Width;
69+
Height = img.Height;
70+
ImageData = data.ToArray();
7171
}
72-
return true;
73-
}
74-
catch
75-
{
76-
Fail();
77-
return false;
7872
}
73+
return true;
7974
}
8075

8176
private void Fail()

0 commit comments

Comments
 (0)