示例#1
0
        /// <summary>
        /// Download the FavIcon as a Bitmap
        /// </summary>
        /// <param name="baseUri"></param>
        /// <returns>Bitmap with the FavIcon</returns>
        public static Bitmap DownloadFavIcon(Uri baseUri)
        {
            Uri url = new Uri(baseUri, new Uri("favicon.ico"));

            try {
                HttpWebRequest request = CreateWebRequest(url);
                using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                {
                    if (request.HaveResponse)
                    {
                        using (Stream responseStream = response.GetResponseStream())
                        {
                            if (responseStream != null)
                            {
                                using (Image image = ImageHelper.FromStream(responseStream))
                                {
                                    return(image.Height > 16 && image.Width > 16 ? new Bitmap(image, 16, 16) : new Bitmap(image));
                                }
                            }
                        }
                    }
                }
            } catch (Exception e) {
                Log.Error("Problem downloading the FavIcon from: " + baseUri, e);
            }
            return(null);
        }
示例#2
0
        /// <summary>
        /// Download the uri to Bitmap
        /// </summary>
        /// <param name="url">Of an image</param>
        /// <returns>Bitmap</returns>
        public static Image DownloadImage(string url)
        {
            StringBuilder extensions = new StringBuilder();

            foreach (var extension in ImageHelper.StreamConverters.Keys)
            {
                if (string.IsNullOrEmpty(extension))
                {
                    continue;
                }
                extensions.AppendFormat(@"\.{0}|", extension);
            }
            extensions.Length--;

            var imageUrlRegex = new Regex($@"(http|https)://.*(?<extension>{extensions})");
            var match         = imageUrlRegex.Match(url);

            try
            {
                using (var memoryStream = GetAsMemoryStream(url))
                {
                    try
                    {
                        return(ImageHelper.FromStream(memoryStream, match.Success ? match.Groups["extension"]?.Value : null));
                    }
                    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.
                        string content;
                        using (StreamReader streamReader = new StreamReader(memoryStream, Encoding.UTF8, true))
                        {
                            content = streamReader.ReadLine();
                        }
                        if (string.IsNullOrEmpty(content))
                        {
                            throw;
                        }
                        match = imageUrlRegex.Match(content);
                        if (!match.Success)
                        {
                            throw;
                        }
                        using (var memoryStream2 = GetAsMemoryStream(match.Value))
                        {
                            return(ImageHelper.FromStream(memoryStream2, match.Groups["extension"]?.Value));
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Log.Error("Problem downloading the image from: " + url, e);
            }
            return(null);
        }
示例#3
0
 /// <summary>
 /// Check if the IDataObject has an image
 /// </summary>
 /// <param name="dataObject"></param>
 /// <returns>true if an image is there</returns>
 public static bool ContainsImage(IDataObject dataObject)
 {
     if (dataObject != null)
     {
         if (dataObject.GetDataPresent(DataFormats.Bitmap) ||
             dataObject.GetDataPresent(DataFormats.Dib) ||
             dataObject.GetDataPresent(DataFormats.Tiff) ||
             dataObject.GetDataPresent(DataFormats.EnhancedMetafile) ||
             dataObject.GetDataPresent(FORMAT_PNG) ||
             dataObject.GetDataPresent(FORMAT_17) ||
             dataObject.GetDataPresent(FORMAT_JPG) ||
             dataObject.GetDataPresent(FORMAT_GIF))
         {
             return(true);
         }
         var imageFiles = GetImageFilenames(dataObject);
         if (imageFiles.Any())
         {
             return(true);
         }
         if (dataObject.GetDataPresent(FORMAT_FILECONTENTS))
         {
             try {
                 MemoryStream imageStream = dataObject.GetData(FORMAT_FILECONTENTS) as MemoryStream;
                 if (IsValidStream(imageStream))
                 {
                     using (ImageHelper.FromStream(imageStream))
                     {
                         // If we get here, there is an image
                         return(true);
                     }
                 }
             } catch (Exception) {
                 // Ignore
             }
         }
     }
     return(false);
 }
示例#4
0
        /// <summary>
        /// Helper method to try to get an image in the specified format from the dataObject
        /// the DIB reader should solve some issues
        /// It also supports Format17/DibV5, by using the following information: http://stackoverflow.com/a/14335591
        /// </summary>
        /// <param name="format">string with the format</param>
        /// <param name="dataObject">IDataObject</param>
        /// <returns>Image or null</returns>
        private static Image GetImageForFormat(string format, IDataObject dataObject)
        {
            object       clipboardObject = GetFromDataObject(dataObject, format);
            MemoryStream imageStream     = clipboardObject as MemoryStream;

            if (!IsValidStream(imageStream))
            {
                // TODO: add "HTML Format" support here...
                return(clipboardObject as Image);
            }
            if (CoreConfig.EnableSpecialDIBClipboardReader)
            {
                if (format == FORMAT_17 || format == DataFormats.Dib)
                {
                    Log.Info("Found DIB stream, trying to process it.");
                    try {
                        if (imageStream != null)
                        {
                            byte[] dibBuffer = new byte[imageStream.Length];
                            imageStream.Read(dibBuffer, 0, dibBuffer.Length);
                            BITMAPINFOHEADER infoHeader = BinaryStructHelper.FromByteArray <BITMAPINFOHEADER>(dibBuffer);
                            if (!infoHeader.IsDibV5)
                            {
                                Log.InfoFormat("Using special DIB <v5 format reader with biCompression {0}", infoHeader.biCompression);
                                int  fileHeaderSize = Marshal.SizeOf(typeof(BITMAPFILEHEADER));
                                uint infoHeaderSize = infoHeader.biSize;
                                int  fileSize       = (int)(fileHeaderSize + infoHeader.biSize + infoHeader.biSizeImage);

                                BITMAPFILEHEADER fileHeader = new BITMAPFILEHEADER
                                {
                                    bfType      = BITMAPFILEHEADER.BM,
                                    bfSize      = fileSize,
                                    bfReserved1 = 0,
                                    bfReserved2 = 0,
                                    bfOffBits   = (int)(fileHeaderSize + infoHeaderSize + infoHeader.biClrUsed * 4)
                                };

                                byte[] fileHeaderBytes = BinaryStructHelper.ToByteArray(fileHeader);

                                using (MemoryStream bitmapStream = new MemoryStream()) {
                                    bitmapStream.Write(fileHeaderBytes, 0, fileHeaderSize);
                                    bitmapStream.Write(dibBuffer, 0, dibBuffer.Length);
                                    bitmapStream.Seek(0, SeekOrigin.Begin);
                                    var image = ImageHelper.FromStream(bitmapStream);
                                    if (image != null)
                                    {
                                        return(image);
                                    }
                                }
                            }
                            else
                            {
                                Log.Info("Using special DIBV5 / Format17 format reader");
                                // CF_DIBV5
                                IntPtr gcHandle = IntPtr.Zero;
                                try {
                                    GCHandle handle = GCHandle.Alloc(dibBuffer, GCHandleType.Pinned);
                                    gcHandle = GCHandle.ToIntPtr(handle);
                                    return
                                        (new Bitmap(infoHeader.biWidth, infoHeader.biHeight,
                                                    -(int)(infoHeader.biSizeImage / infoHeader.biHeight),
                                                    infoHeader.biBitCount == 32?PixelFormat.Format32bppArgb: PixelFormat.Format24bppRgb,
                                                    new IntPtr(handle.AddrOfPinnedObject().ToInt32() + infoHeader.OffsetToPixels + (infoHeader.biHeight - 1) * (int)(infoHeader.biSizeImage / infoHeader.biHeight))
                                                    ));
                                } catch (Exception ex) {
                                    Log.Error("Problem retrieving Format17 from clipboard.", ex);
                                } finally {
                                    if (gcHandle == IntPtr.Zero)
                                    {
                                        GCHandle.FromIntPtr(gcHandle).Free();
                                    }
                                }
                            }
                        }
                    } catch (Exception dibEx) {
                        Log.Error("Problem retrieving DIB from clipboard.", dibEx);
                    }
                }
            }
            else
            {
                Log.Info("Skipping special DIB format reader as it's disabled in the configuration.");
            }
            try {
                if (imageStream != null)
                {
                    imageStream.Seek(0, SeekOrigin.Begin);
                    var tmpImage = ImageHelper.FromStream(imageStream);
                    if (tmpImage != null)
                    {
                        Log.InfoFormat("Got image with clipboard format {0} from the clipboard.", format);
                        return(tmpImage);
                    }
                }
            } catch (Exception streamImageEx) {
                Log.Error($"Problem retrieving {format} from clipboard.", streamImageEx);
            }
            return(null);
        }