Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
liesauer committed Apr 8, 2022
1 parent 49ae7a5 commit fe390b9
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 56 deletions.
121 changes: 67 additions & 54 deletions pic2meme/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public partial class MainWindow : Window
{
private bool _init;
private IDataObject currentDataObject;
private string sourceImage;

public MainWindow()
{
Expand All @@ -44,7 +45,7 @@ private void Window_Drop(object sender, DragEventArgs e)
private void HandleDataObject(IDataObject dataObject)
{
currentDataObject = dataObject;
pic2meme();
pic2meme(true);
}

private int GetCovertMode()
Expand All @@ -65,7 +66,7 @@ private int GetSizeMode()
return 0;
}

private async void pic2meme()
private async void pic2meme(bool keepOriginal = false)
{
if (!_init) return;

Expand Down Expand Up @@ -99,87 +100,99 @@ private async void pic2meme()

var sourceImage = "";

try
if (keepOriginal || string.IsNullOrEmpty(this.sourceImage))
{
if (currentDataObject.GetDataPresent(DataFormats.Html))
try
{
var html = currentDataObject.GetData(DataFormats.Html) as string;

if (string.IsNullOrEmpty(html))
if (currentDataObject.GetDataPresent(DataFormats.Html))
{
Notice.Content = $"转换失败:无法读取网络图片";
var html = currentDataObject.GetData(DataFormats.Html) as string;

return;
}
if (string.IsNullOrEmpty(html))
{
Notice.Content = $"转换失败:无法读取网络图片";

return;
}

var sourceUrl = "";
var sourceUrl = "";

var sourceUrlIndex = html.IndexOf("SourceURL:");
if (sourceUrlIndex != -1)
{
var sourceUrlIndex2 = html.IndexOf("\r\n", sourceUrlIndex);
if (sourceUrlIndex2 != -1)
var sourceUrlIndex = html.IndexOf("SourceURL:");
if (sourceUrlIndex != -1)
{
var startPos = sourceUrlIndex + "SourceURL:".Length;
sourceUrl = html.Substring(startPos, sourceUrlIndex2 - startPos);
var sourceUrlIndex2 = html.IndexOf("\r\n", sourceUrlIndex);
if (sourceUrlIndex2 != -1)
{
var startPos = sourceUrlIndex + "SourceURL:".Length;
sourceUrl = html.Substring(startPos, sourceUrlIndex2 - startPos);
}
}
}

var body = "";
var body = "";

var bodyIndex = html.IndexOf("<!--StartFragment-->");
if (bodyIndex != -1)
{
var startPos = bodyIndex + "<!--StartFragment-->".Length;

var bodyIndex2 = html.IndexOf("<!--EndFragment-->", startPos);
if (bodyIndex2 != -1)
var bodyIndex = html.IndexOf("<!--StartFragment-->");
if (bodyIndex != -1)
{
body = html.Substring(startPos, bodyIndex2 - startPos);
var startPos = bodyIndex + "<!--StartFragment-->".Length;

var bodyIndex2 = html.IndexOf("<!--EndFragment-->", startPos);
if (bodyIndex2 != -1)
{
body = html.Substring(startPos, bodyIndex2 - startPos);
}
}
}

if (string.IsNullOrEmpty(body))
{
Notice.Content = $"转换失败:无法读取网络图片";
if (string.IsNullOrEmpty(body))
{
Notice.Content = $"转换失败:无法读取网络图片";

return;
}
return;
}

var imgUrl = "";
var imgUrl = "";

var imgIndex = body.IndexOf(@"<img src=""");
if (imgIndex != -1)
{
var startPos = imgIndex + @"<img src=""".Length;
var imgIndex = body.IndexOf(@"<img src=""");
if (imgIndex != -1)
{
var startPos = imgIndex + @"<img src=""".Length;

var imgIndex2 = body.IndexOf(@"""", startPos);
if (imgIndex2 != -1)
var imgIndex2 = body.IndexOf(@"""", startPos);
if (imgIndex2 != -1)
{
imgUrl = body.Substring(startPos, imgIndex2 - startPos);
}
}

using (WebClient client = new WebClient())
{
imgUrl = body.Substring(startPos, imgIndex2 - startPos);
var tmp = GenerateTempFile(".gif");
await client.DownloadFileTaskAsync(imgUrl, tmp);
sourceImage = tmp;
}
}

using (WebClient client = new WebClient())
else if (currentDataObject.GetDataPresent(DataFormats.FileDrop))
{
var tmp = GenerateTempFile(".gif");
await client.DownloadFileTaskAsync(imgUrl, tmp);
sourceImage = tmp;
sourceImage = ((System.Array)currentDataObject.GetData(DataFormats.FileDrop)).GetValue(0).ToString();
}
else if (currentDataObject.GetDataPresent(DataFormats.Bitmap))
{
var image = currentDataObject.GetData(DataFormats.Bitmap) as BitmapSource;
sourceImage = SaveBitmapSource(image);
}
}
else if (currentDataObject.GetDataPresent(DataFormats.FileDrop))
catch
{
sourceImage = ((System.Array)currentDataObject.GetData(DataFormats.FileDrop)).GetValue(0).ToString();
sourceImage = null;
}
else if (currentDataObject.GetDataPresent(DataFormats.Bitmap))

if (keepOriginal)
{
var image = currentDataObject.GetData(DataFormats.Bitmap) as BitmapSource;
sourceImage = SaveBitmapSource(image);
this.sourceImage = sourceImage;
}
}
catch
else
{
sourceImage = null;
sourceImage = this.sourceImage;
}

if (string.IsNullOrEmpty(sourceImage))
Expand Down
4 changes: 2 additions & 2 deletions pic2meme/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public static Task<bool> Any2GIF(string filePath, string savePath, int forceSize
int max = Math.Max(image.Width, image.Height);
if (forceSize > 0 && max != forceSize)
{
float scale = max / forceSize;
float scale = (float)max / (float)forceSize;
image.Mutate(x => x.Resize((int)(image.Width / scale), (int)(image.Height / scale)));
}
image.SaveAsGif(savePath);
Expand All @@ -111,7 +111,7 @@ public static Task<bool> Any2GIF2(string filePath, string savePath, int forceSiz
int max = Math.Max(image.Width, image.Height);
if (forceSize > 0 && max != forceSize)
{
float scale = max / forceSize;
float scale = (float)max / (float)forceSize;
image.Mutate(x => x.Resize((int)(image.Width / scale), (int)(image.Height / scale)));
}
var gifEncoder = new GifEncoder();
Expand Down

0 comments on commit fe390b9

Please sign in to comment.