예제 #1
0
    public static IFileObject SaveAndResizeImage(Stream input, IFileSystem fs, IFileObject file, int maxWidth, int maxHeight, ImageFormat saveAsFormat )
    {
        Image img;
        MemoryStream imgSrcStream = new MemoryStream();
        byte[] buf = new byte[1024*50];
        int bufRead = 0;
        do {
            bufRead = input.Read(buf, 0, buf.Length);
            if (bufRead>0)
                imgSrcStream.Write(buf, 0, bufRead);
        } while (bufRead>0);
        imgSrcStream.Position = 0;

        try {
            img = Image.FromStream(imgSrcStream);
        } catch (Exception ex) {
            throw new Exception( WebManager.GetLabel("Invalid image format") );
        }
        if (img.Size.Width==0 || img.Size.Height==0)
            throw new Exception( WebManager.GetLabel("Invalid image size") );

        var sizeIsWidthOk = (maxWidth<=0 || img.Size.Width<=maxWidth);
        var sizeIsHeightOk = (maxHeight<=0 || img.Size.Height<=maxHeight);
        var sizeIsOk = sizeIsWidthOk && sizeIsHeightOk;

        var originalImgFmt = ResolveImageFormat( Path.GetExtension(file.Name) ) ?? ImageFormat.Bmp;
        var formatIsOk = (saveAsFormat==null && !originalImgFmt.Equals(ImageFormat.Bmp) && !originalImgFmt.Equals(ImageFormat.Tiff) ) || originalImgFmt.Equals(saveAsFormat);

        if (!formatIsOk || !sizeIsOk ) {
            var saveAsFormatResolved = saveAsFormat!=null ? saveAsFormat : (originalImgFmt==ImageFormat.Jpeg?ImageFormat.Jpeg:ImageFormat.Png);
            var newFmtExtension = GetImageFormatExtension(saveAsFormatResolved);

            var newFile = fs.ResolveFile( file.Name + (Path.GetExtension(file.Name).ToLower()==newFmtExtension ? String.Empty : newFmtExtension) );
            newFile.CreateFile();

            if (!sizeIsOk) {
                var newWidth = img.Size.Width;
                var newHeight = img.Size.Height;
                if (!sizeIsWidthOk) {
                    newWidth = maxWidth;
                    newHeight = (int) Math.Floor( ((double)img.Size.Height)*( ((double)maxWidth)/((double)img.Size.Width) )  );
                    if ( maxHeight<0 || newHeight<=maxHeight )
                        sizeIsHeightOk = true;
                }
                if (!sizeIsHeightOk) {
                    newHeight = maxHeight;
                    newWidth = (int) Math.Floor( ((double)img.Size.Width)*( ((double)maxHeight)/((double)img.Size.Height) )  );
                }
                var resizedBitmap = new Bitmap(img, newWidth, newHeight);

                var imageProps = img.PropertyItems;
                    foreach (PropertyItem propItem in imageProps){
                    resizedBitmap.SetPropertyItem(propItem);
                }

                SaveImage(resizedBitmap, newFile.GetContent().OutputStream, saveAsFormatResolved);

            } else {
                SaveImage(img, newFile.GetContent().OutputStream, saveAsFormatResolved );
            }
            newFile.Close();
            return newFile;
        }
        file.CreateFile();
        imgSrcStream.Position = 0;
        file.CopyFrom( imgSrcStream );
        file.Close();
        return file;
    }
예제 #2
0
    public static IFileObject SaveAndResizeImage(Stream input, IFileSystem fs, IFileObject file, int maxWidth, int maxHeight, ImageFormat saveAsFormat)
    {
        Image        img;
        MemoryStream imgSrcStream = new MemoryStream();

        byte[] buf     = new byte[1024 * 50];
        int    bufRead = 0;

        do
        {
            bufRead = input.Read(buf, 0, buf.Length);
            if (bufRead > 0)
            {
                imgSrcStream.Write(buf, 0, bufRead);
            }
        } while (bufRead > 0);
        imgSrcStream.Position = 0;

        try {
            img = Image.FromStream(imgSrcStream);
        } catch (Exception ex) {
            throw new Exception(WebManager.GetLabel("Invalid image format"));
        }
        if (img.Size.Width == 0 || img.Size.Height == 0)
        {
            throw new Exception(WebManager.GetLabel("Invalid image size"));
        }

        var sizeIsWidthOk  = (maxWidth <= 0 || img.Size.Width <= maxWidth);
        var sizeIsHeightOk = (maxHeight <= 0 || img.Size.Height <= maxHeight);
        var sizeIsOk       = sizeIsWidthOk && sizeIsHeightOk;

        var originalImgFmt = ResolveImageFormat(Path.GetExtension(file.Name)) ?? ImageFormat.Bmp;
        var formatIsOk     = (saveAsFormat == null && !originalImgFmt.Equals(ImageFormat.Bmp) && !originalImgFmt.Equals(ImageFormat.Tiff)) || originalImgFmt.Equals(saveAsFormat);

        if (!formatIsOk || !sizeIsOk)
        {
            var saveAsFormatResolved = saveAsFormat != null ? saveAsFormat : (originalImgFmt == ImageFormat.Jpeg?ImageFormat.Jpeg:ImageFormat.Png);
            var newFmtExtension      = GetImageFormatExtension(saveAsFormatResolved);

            var newFile = fs.ResolveFile(file.Name + (Path.GetExtension(file.Name).ToLower() == newFmtExtension ? String.Empty : newFmtExtension));
            newFile.CreateFile();

            if (!sizeIsOk)
            {
                var newWidth  = img.Size.Width;
                var newHeight = img.Size.Height;
                if (!sizeIsWidthOk)
                {
                    newWidth  = maxWidth;
                    newHeight = (int)Math.Floor(((double)img.Size.Height) * (((double)maxWidth) / ((double)img.Size.Width)));
                    if (maxHeight < 0 || newHeight <= maxHeight)
                    {
                        sizeIsHeightOk = true;
                    }
                }
                if (!sizeIsHeightOk)
                {
                    newHeight = maxHeight;
                    newWidth  = (int)Math.Floor(((double)img.Size.Width) * (((double)maxHeight) / ((double)img.Size.Height)));
                }
                var resizedBitmap = new Bitmap(img, newWidth, newHeight);

                var imageProps = img.PropertyItems;
                foreach (PropertyItem propItem in imageProps)
                {
                    resizedBitmap.SetPropertyItem(propItem);
                }

                SaveImage(resizedBitmap, newFile.GetContent().OutputStream, saveAsFormatResolved);
            }
            else
            {
                SaveImage(img, newFile.GetContent().OutputStream, saveAsFormatResolved);
            }
            newFile.Close();
            return(newFile);
        }
        file.CreateFile();
        imgSrcStream.Position = 0;
        file.CopyFrom(imgSrcStream);
        file.Close();
        return(file);
    }