mirror of
https://github.com/greenshot/greenshot
synced 2025-07-16 10:03:44 -07:00
FEATURE-838: Added support for dragging an image from google image search results directly into the editor.
This commit is contained in:
parent
ea8c27449f
commit
5d8ad4d021
2 changed files with 53 additions and 7 deletions
|
@ -86,6 +86,26 @@ namespace GreenshotPlugin.Core {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Download the uri into a memorystream, without catching exceptions
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="url">Of an image</param>
|
||||||
|
/// <returns>MemoryStream which is already seeked to 0</returns>
|
||||||
|
public static MemoryStream GetAsMemoryStream(string url) {
|
||||||
|
HttpWebRequest request = CreateWebRequest(url);
|
||||||
|
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) {
|
||||||
|
MemoryStream memoryStream = new MemoryStream();
|
||||||
|
using (Stream responseStream = response.GetResponseStream()) {
|
||||||
|
if (responseStream != null) {
|
||||||
|
responseStream.CopyTo(memoryStream);
|
||||||
|
}
|
||||||
|
// Make sure it can be used directly
|
||||||
|
memoryStream.Seek(0, SeekOrigin.Begin);
|
||||||
|
}
|
||||||
|
return memoryStream;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Download the uri to Bitmap
|
/// Download the uri to Bitmap
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -93,16 +113,28 @@ namespace GreenshotPlugin.Core {
|
||||||
/// <returns>Bitmap</returns>
|
/// <returns>Bitmap</returns>
|
||||||
public static Image DownloadImage(string url) {
|
public static Image DownloadImage(string url) {
|
||||||
try {
|
try {
|
||||||
HttpWebRequest request = CreateWebRequest(url);
|
string content;
|
||||||
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
|
using (MemoryStream memoryStream = GetAsMemoryStream(url)) {
|
||||||
if (request.HaveResponse) {
|
try {
|
||||||
using (Stream responseStream = response.GetResponseStream()) {
|
using (Image image = Image.FromStream(memoryStream)) {
|
||||||
if (responseStream != null) {
|
return ImageHelper.Clone(image, PixelFormat.Format32bppArgb);
|
||||||
using (Image image = Image.FromStream(responseStream)) {
|
}
|
||||||
|
} catch (Exception) {
|
||||||
|
// If we arrive here, the image loading didn't work, try to see if the response has a http(s) URL to an image and just take this instead.
|
||||||
|
using (StreamReader streamReader = new StreamReader(memoryStream, Encoding.UTF8, true)) {
|
||||||
|
content = streamReader.ReadLine();
|
||||||
|
}
|
||||||
|
Regex imageUrlRegex = new Regex(@"(http|https)://.*(\.png|\.gif|\.jpg|\.tiff|\.jpeg|\.bmp)");
|
||||||
|
Match match = imageUrlRegex.Match(content);
|
||||||
|
if (match.Success) {
|
||||||
|
using (MemoryStream memoryStream2 = GetAsMemoryStream(match.Value)) {
|
||||||
|
using (Image image = Image.FromStream(memoryStream2)) {
|
||||||
return ImageHelper.Clone(image, PixelFormat.Format32bppArgb);
|
return ImageHelper.Clone(image, PixelFormat.Format32bppArgb);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
throw;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
LOG.Error("Problem downloading the image from: " + url, e);
|
LOG.Error("Problem downloading the image from: " + url, e);
|
||||||
|
|
|
@ -148,5 +148,19 @@ namespace GreenshotPlugin.Core {
|
||||||
|
|
||||||
return returnValue;
|
return returnValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Read "streamextensions" :)
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="input">Stream</param>
|
||||||
|
/// <param name="output">Stream</param>
|
||||||
|
public static void CopyTo(this Stream input, Stream output) {
|
||||||
|
byte[] buffer = new byte[16 * 1024]; // Fairly arbitrary size
|
||||||
|
int bytesRead;
|
||||||
|
|
||||||
|
while ((bytesRead = input.Read(buffer, 0, buffer.Length)) > 0) {
|
||||||
|
output.Write(buffer, 0, bytesRead);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue