예제 #1
0
        public static Bitmap GetThumbnail(Bitmap bmpImage, Size size)
        {
            double width = bmpImage.Width;
             double height = bmpImage.Height;

             Double WidthToHeightAspect = (double)bmpImage.Width / (double)bmpImage.Height;

             Double xFactor = (double)size.Width / width;
             Double yFactor = (double)size.Height / height;
             Size scaledSize = new Size();
             if (xFactor < yFactor)
             {
            scaledSize.Width = size.Width;
            scaledSize.Height = Convert.ToInt32(size.Width / WidthToHeightAspect);
             }
             else
             {
            scaledSize.Width = Convert.ToInt32(size.Height * WidthToHeightAspect);
            scaledSize.Height = size.Height;
             }

             Bitmap scaledOriginal;
             Bitmap thumnailBmp = new Bitmap(size.Width, size.Height);
             // Compact framework can't resize bitmaps!
             // Using OpenNETCF instead
             using (MemoryStream ms = new MemoryStream())
             {
            ImagingFactoryClass imageFactory = new ImagingFactoryClass();

            IImage image;
            IBitmapImage iBitmap;

            ImageInfo info;

            bmpImage.Save(ms, ImageFormat.Png);
            imageFactory.CreateImageFromStream(new StreamOnFile(ms), out image);
            image.GetImageInfo(out info);
            imageFactory.CreateBitmapFromImage(image, (uint)scaledSize.Width, (uint)scaledSize.Height,
                info.PixelFormat, InterpolationHint.InterpolationHintDefault, out iBitmap);

            //Set up the scaled bitmap to be copied
            scaledOriginal = ImageUtils.IBitmapImageToBitmap(iBitmap);

            for (int y = 0; y < thumnailBmp.Height; y++)
            {
               for (int x = 0; x < thumnailBmp.Width; x++)
               {
                  if ((x < scaledOriginal.Width) && (y < scaledOriginal.Height))
                  {
                     thumnailBmp.SetPixel(x, y, scaledOriginal.GetPixel(x, y));
                  }
                  else
                  {
                     thumnailBmp.SetPixel(x, y, Color.Transparent);
                  }
               }
            }
             }
             return thumnailBmp;
        }
예제 #2
0
        // Loads image from a specified url
        private void LoadImage(string url)
        {
            HttpWebRequest  rq  = HttpWebRequest.Create(url) as HttpWebRequest;
            HttpWebResponse rsp = null;

            try
            {
                // Request data from the url
                rsp = rq.GetResponse() as HttpWebResponse;
                Stream st = rsp.GetResponseStream();

                // Read all the image data to memory
                // If we run out of memory, so be it.
                ms = new MemoryStream();
                byte[] buffer = new byte[1024];
                int    cb     = 0;
                while ((cb = st.Read(buffer, 0, buffer.Length)) > 0)
                {
                    ms.Write(buffer, 0, cb);
                }
                st.Close();
                rsp.Close();

                // Use ImageFactory class to load image
                _stream = new StreamOnFile(ms);
                if (_stream != null)
                {
                    IImagingFactory factory = new ImagingFactoryClass();
                    factory.CreateImageFromStream(_stream, out _image);
                }
            }
            catch (WebException wex)
            {
                if (wex.Response != null)
                {
                    wex.Response.Close();
                }
                throw;
            }
            catch
            {
                MessageBox.Show("Unable to load image");
            }
            finally
            {
                if (rsp != null)
                {
                    rsp.Close();
                }
            }
        }
예제 #3
0
 public static IBitmapImage CreateThumbnail(Stream stream, Size size)
 {
     IBitmapImage imageBitmap;
     ImageInfo ii;
     IImage image;
     ImagingFactory factory = new ImagingFactoryClass();
     factory.CreateImageFromStream(
     new StreamOnFile(stream),
     out image);
     image.GetImageInfo(out ii);
     factory.CreateBitmapFromImage(
         image,
         (uint)size.Width,
         (uint)size.Height,
         ii.PixelFormat,
         InterpolationHint.InterpolationHintDefault,
         out imageBitmap);
     return imageBitmap;
 }
예제 #4
0
        static public IBitmapImage CreateGrayScaleImage(Stream stream)
        {
            ImagingFactory factory = new ImagingFactoryClass();
            IImage         image;

            factory.CreateImageFromStream(new StreamOnFile(stream), out image);
            ImageInfo ii;

            image.GetImageInfo(out ii);

            image.SetImageFlags((ImageFlags)((int)ii.Flags & (int)ImageFlags.ImageFlagsColorSpaceGRAY));
            IBitmapImage imageBitmap = null;

            try
            {
                factory.CreateBitmapFromImage(image, (uint)ii.Width, (uint)ii.Height, ii.PixelFormat, InterpolationHint.InterpolationHintDefault, out imageBitmap);
            }
            catch { }
            return(imageBitmap);
        }
예제 #5
0
파일: ImageClass.cs 프로젝트: xorkrus/vk_wm
        public static bool ShrinkToMaxLinearSize(string fileName, uint maxLinearSize)
        {
            IImagingFactory newIImagingFactory = new ImagingFactoryClass();
            IImage          newIImage          = null;
            IImage          newIImageThumb     = null;
            ImageInfo       newImageInfo;

            uint imageThumbWidth  = 0;
            uint imageThumbHeight = 0;

            if (!File.Exists(fileName))
            {
                return(false);
            }

            try
            {
                using (var newStreamOnFile = new StreamOnFile(fileName))
                {
                    newIImagingFactory.CreateImageFromStream(newStreamOnFile, out newIImage);

                    newIImage.GetImageInfo(out newImageInfo);

                    uint imageWidth  = newImageInfo.Width;
                    uint imageHeight = newImageInfo.Height;

                    if (imageWidth > maxLinearSize || imageHeight > maxLinearSize)
                    {
                        float wa = (float)imageWidth / (float)maxLinearSize;
                        float ha = (float)imageHeight / (float)maxLinearSize;

                        float a = (float)imageWidth / (float)imageHeight;

                        if (wa > ha)
                        {
                            imageThumbWidth  = maxLinearSize;
                            imageThumbHeight = (uint)(maxLinearSize / a);
                        }
                        else
                        {
                            imageThumbHeight = maxLinearSize;
                            imageThumbWidth  = (uint)(maxLinearSize * a);
                        }

                        newIImage.GetThumbnail(imageThumbWidth, imageThumbHeight, out newIImageThumb);

                        Marshal.FinalReleaseComObject(newIImage);
                    }
                }
            }
            catch
            {
                return(false);
            }

            try
            {
                File.Delete(fileName);

                IBitmapImage newIBitmapImage;
                newIImagingFactory.CreateBitmapFromImage(newIImageThumb, imageThumbWidth, imageThumbHeight, newImageInfo.PixelFormat, InterpolationHint.InterpolationHintBilinear, out newIBitmapImage);
                Bitmap newBitmap = ImageUtils.IBitmapImageToBitmap(newIBitmapImage);

                newBitmap.Save(fileName, System.Drawing.Imaging.ImageFormat.Jpeg);
            }
            catch
            {
                return(false);
            }

            return(true);
        }
예제 #6
0
파일: ImageClass.cs 프로젝트: xorkrus/vk_wm
        public static bool ShrinkToMaxLinearSize(string fileName, uint maxLinearSize)
        {
            IImagingFactory newIImagingFactory = new ImagingFactoryClass();
            IImage newIImage = null;
            IImage newIImageThumb = null;
            ImageInfo newImageInfo;

            uint imageThumbWidth = 0;
            uint imageThumbHeight = 0;

            if (!File.Exists(fileName))
            {
                return false;
            }

            try
            {
                using (var newStreamOnFile = new StreamOnFile(fileName))
                {
                    newIImagingFactory.CreateImageFromStream(newStreamOnFile, out newIImage);

                    newIImage.GetImageInfo(out newImageInfo);

                    uint imageWidth = newImageInfo.Width;
                    uint imageHeight = newImageInfo.Height;

                    if (imageWidth > maxLinearSize || imageHeight > maxLinearSize)
                    {
                        float wa = (float)imageWidth / (float)maxLinearSize;
                        float ha = (float)imageHeight / (float)maxLinearSize;

                        float a = (float)imageWidth / (float)imageHeight;

                        if (wa > ha)
                        {
                            imageThumbWidth = maxLinearSize;
                            imageThumbHeight = (uint)(maxLinearSize / a);
                        }
                        else
                        {
                            imageThumbHeight = maxLinearSize;
                            imageThumbWidth = (uint)(maxLinearSize * a);
                        }

                        newIImage.GetThumbnail(imageThumbWidth, imageThumbHeight, out newIImageThumb);

                        Marshal.FinalReleaseComObject(newIImage);
                    }
                }
            }
            catch
            {
                return false;
            }

            try
            {
                File.Delete(fileName);

                IBitmapImage newIBitmapImage;
                newIImagingFactory.CreateBitmapFromImage(newIImageThumb, imageThumbWidth, imageThumbHeight, newImageInfo.PixelFormat, InterpolationHint.InterpolationHintBilinear, out newIBitmapImage);
                Bitmap newBitmap = ImageUtils.IBitmapImageToBitmap(newIBitmapImage);

                newBitmap.Save(fileName, System.Drawing.Imaging.ImageFormat.Jpeg);
            }
            catch
            {
                return false;
            }

            return true;
        }