C#更改上传图像的dpi

前端之家收集整理的这篇文章主要介绍了C#更改上传图像的dpi前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我必须遵循被调用来改变图像分辨率的函数.我想这样做,所以上传的图像例如300dpi将被修改为72dpi(对于web).这个问题与我在这里工作的 another question有关.

我创建了一个扩展方法,以便能够在我的应用程序中的更多位置使用此功能,而不是仅在上载新文件时. (见上述问题)

public static byte[] SetDpiTo72(this byte[] imageToFit,string mimeType,Size newSize)
{    
    using (MemoryStream memoryStream = new MemoryStream(),newMemoryStream = new MemoryStream())
    {
        memoryStream.Write(imageToFit,imageToFit.Length);
        var originalImage = new Bitmap(memoryStream);

        using (var canvas = Graphics.FromImage(originalImage))
        {
            canvas.SmoothingMode = SmoothingMode.AntiAlias;
            canvas.InterpolationMode = InterpolationMode.HighQualityBicubic;
            canvas.PixelOffsetMode = PixelOffsetMode.HighQuality;
            canvas.DrawImage((Image)originalImage,newSize.Width,newSize.Height);

            newBitmap.SetResolution(72,72);
            newBitmap.Save(newMemoryStream,ImageFunctions.GetEncoderInfo(mimeType),null);
        }
        return newMemoryStream.ToArray();
    }
}

提到的扩展方法在类似于下面情况的函数中被调用;

if (newSize.Width > originalImage.Width && newSize.Height > originalImage.Height)
{
     newSize.Width = originalImage.Width;
     newSize.Height = originalImage.Height;

     uploadedFileBuffer = uploadedFileBuffer.SetDpiTo72(uploadedFile.ContentType,newSize);

     return CreateFile(newSize,uploadedFile,uploadedFileBuffer);
}

进入的bytearray是一个bytearray文件.它已经具有正确的大小,但我想将分辨率更改为72dpi.但是,在执行exectution并保存图像后,分辨率仍然是原始输入的分辨率,即300dpi.我怎样才能做到这一点?

几个答案后的更新:

public static byte[] SetDpiTo72(this byte[] imageToFit,Size newSize)
        {
            using (MemoryStream memoryStream = new MemoryStream(),newMemoryStream = new MemoryStream())
            {
                memoryStream.Write(imageToFit,imageToFit.Length);
                var originalImage = new Bitmap(memoryStream);

                using (var canvas = Graphics.FromImage(originalImage))
                {
                    canvas.SmoothingMode = SmoothingMode.AntiAlias;
                    canvas.InterpolationMode = InterpolationMode.HighQualityBicubic;
                    canvas.PixelOffsetMode = PixelOffsetMode.HighQuality;
                    canvas.DrawImage((Image)originalImage,newSize.Height);

                    originalImage.SetResolution(72,72);

                    var epQuality = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality,75);
                    var epParameters = new EncoderParameters(1);
                    epParameters.Param[0] = epQuality;

                    Image newimg = Image.FromStream(memoryStream);

                    //Getting an GDI+ exception after the execution of this line.
                    newimg.Save("C:\\test1234.jpg",epParameters);

                    originalImage.Save("test.jpg",ImageFormat.Jpeg);

                    //This line give me an Argumentexception - Parameter is not valid.
                    //originalImage.Save(newMemoryStream,epParameters);
                    //newMemoryStream.Close();
                }
                return newMemoryStream.ToArray();
            }
        }

除了异常之外的stackstrace告诉我以下内容;

at System.Drawing.Image.Save(String filename,ImageCodecInfo encoder,EncoderParameters encoderParams)
   at Extensions.ByteArrayExtensions.SetDpiTo72(Byte[] imageToFit,String mimeType,Size newSize) in C:\Website\Project\Extensions\ByteArrayExtensions.cs:line 356
   at CMS.Presentation.FileFunctions.CreateFullsizeImage(HttpPostedFileBase uploadedFile,Size newSize,Byte[] uploadedFileBuffer) in C:\Website\Project\CMS.Presentation\FileFunctions.cs:line 197
   at CMS.Presentation.FileFunctions.CreateFile(HttpPostedFileBase uploadedFile,INodeService nodeservice,Guid userId,Node parentNode) in C:\Website\Project\CMS.Presentation\FileFunctions.cs:line 53

与此同时,我还开发了另一个功能(见下文),只调整位图.这似乎工作正常.我不能在我当前的实现中使用此函数,因为它只返回一个Bitmap.或者我应该改变一切以使用位图?

private static Bitmap ResizeImage(Image image,int width,int height)
        {
            var frameCount = image.GetFrameCount(new FrameDimension(image.FrameDimensionsList[0]));
            var newDimensions = ImageFunctions.GenerateImageDimensions(image.Width,image.Height,width,height);
            Bitmap resizedImage;

            if (frameCount > 1)
            {
                //we have a animated GIF
                resizedImage = ResizeAnimatedGifImage(image,height);
            }
            else
            {
                resizedImage = (Bitmap)image.GetThumbnailImage(newDimensions.Width,newDimensions.Height,null,IntPtr.Zero);
            }

            resizedImage.SetResolution(72,72);

            return resizedImage;
        }

解决方法

花了我一会儿,但我终于找到了问题!
问题在于我使用的ResizeImage函数.在’GetThumbnailImage’中要具体.我遇到了另一个模糊图像的问题,这是可以解释的,因为GetThumbnailImage会将创建的ThumbNail拉伸到所需的大小.缩略图的分辨率永远不会改变.
private static Bitmap ResizeImage(Image image,72);

            return resizedImage;
        }

通过将上面的函数修改为下面的函数,我能够使用Graphics.DrawImage在渲染之前重绘新图像来解决问题. GenerateImageDimensions也略有修改.这个问题一起解决了.

private static Bitmap ResizeImage(Image image,height);

            var resizedImage = new Bitmap(newDimensions.Width,newDimensions.Height);
            if (frameCount > 1)
            {
                //we have a animated GIF
                resizedImage = ResizeAnimatedGifImage(image,height);
            }
            else
            {

                //we have a normal image
                using (var gfx = Graphics.FromImage(resizedImage))
                {
                    gfx.SmoothingMode = SmoothingMode.HighQuality;
                    gfx.InterpolationMode = InterpolationMode.HighQualityBicubic;
                    gfx.PixelOffsetMode = PixelOffsetMode.HighQuality;

                    var targRectangle = new Rectangle(0,newDimensions.Width,newDimensions.Height);
                    var srcRectangle = new Rectangle(0,image.Width,image.Height);

                    gfx.DrawImage(image,targRectangle,srcRectangle,GraphicsUnit.Pixel);
                }
            }

            return resizedImage;
        }
原文链接:https://www.f2er.com/csharp/99416.html

猜你在找的C#相关文章