调整图像大小,减少C#的损失

时间:2016-09-20 08:01:31

标签: c#

在我的项目中,我必须通过输入百分比来减少C#程序的图像大小。

  

然而,有些不对劲:

     

图片格式: TIF

     

原始图片 6MB,1920 * 1080

     

已调整大小的图片:27KB,含1728 * 972

     

缩小至 90%

它不是27KB,而是> 5MB ...

代码

    public Image reduce(Image sourceImage, string size)
    {
        var original = sourceImage;
        //store image widths in variable for easier use
        var oW = (decimal)original.Width;
        var oH = (decimal)original.Height;
        var dW = (decimal)desiredWidth;
        var dH = (decimal)desiredHeight;
        var dR = (decimal)Int32.Parse(size);;

        var nH = oH * (dR / 100); 
        var nW = oW * (dR / 100);

        var resized = new Bitmap((int)nW, (int)nH);
        using (Graphics gr = Graphics.FromImage(sourceImage))
        {
            gr.SmoothingMode = SmoothingMode.HighQuality;
            gr.InterpolationMode = InterpolationMode.HighQualityBicubic;
            gr.PixelOffsetMode = PixelOffsetMode.HighQuality;
            gr.DrawImage(resized, new Rectangle(0, 0, (int)nH, (int)nW));
        }

        return resized;
    }

任何想法?

更新

              Image nImage = imagetool.reduce(originalImg, size);

              byte[] nImageData = imagetool.imageToByteArray(nImage);

              int nImageSize = nImageData.Length;
              Console.WriteLine("new image size " + nImageSize );


               try
               {
                  Console.WriteLine("trying to update the image in sharepoint");

                  web.Files.Add(Path.Combine(itemsite, itemSubsite, fileURL), nImageData, true);
               }

更新2

    public byte[] imageToByteArray(Image imageIn)
    {
        using (var ms = new MemoryStream())
        {
            if (imageIn.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Jpeg))
            {
                imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                return ms.ToArray();
            }
            if (imageIn.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Png))
            {
                imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
                return ms.ToArray();
            }
            if (imageIn.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Bmp))
            {
                imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
                return ms.ToArray();
            }
            if (imageIn.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Gif))
            {
                imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
                return ms.ToArray();
            }
            if (imageIn.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Tiff))
            {
                imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Tiff);
                return ms.ToArray();
            }
            else
            {
                imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                return ms.ToArray();
            }
        }
    }

2 个答案:

答案 0 :(得分:0)

<Sep 20, 2016 11:21:40 AM EEST> <Warning> <HTTP> <BEA-101162> <User defined listener org.springframework.web.context.ContextLoaderListener failed: org.springframework.beans.factory.BeanDefinitionStoreException: Unexpected exception parsing XML document from ServletContext resource [/WEB-INF/applicationContext.xml]; nested exception is java.lang.ClassCastException: weblogic.xml.jaxp.RegistryDocumentBuilderFactory cannot be cast to javax.xml.parsers.DocumentBuilderFactory. org.springframework.beans.factory.BeanDefinitionStoreException: Unexpected exception parsing XML document from ServletContext resource [/WEB-INF/applicationContext.xml]; nested exception is java.lang.ClassCastException: weblogic.xml.jaxp.RegistryDocumentBuilderFactory cannot be cast to javax.xml.parsers.DocumentBuilderFactory at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:412) at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:334) at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:302) at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:174) at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:209) Truncated. see log file for complete stacktrace Caused By: java.lang.ClassCastException: weblogic.xml.jaxp.RegistryDocumentBuilderFactory cannot be cast to javax.xml.parsers.DocumentBuilderFactory at javax.xml.parsers.DocumentBuilderFactory.newInstance(Unknown Source) at org.springframework.beans.factory.xml.DefaultDocumentLoader.createDocumentBuilderFactory(DefaultDocumentLoader.java:89) at org.springframework.beans.factory.xml.DefaultDocumentLoader.loadDocument(DefaultDocumentLoader.java:70) at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:388) at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:334) Truncated. see log file for complete stacktrace > <Sep 20, 2016 11:21:40 AM EEST> <Error> <HTTP> <BEA-101216> <Servlet: "spring-dispatcher" failed to preload on startup in Web application: "nsi-client.war". org.springframework.beans.factory.BeanDefinitionStoreException: Unexpected exception parsing XML document from ServletContext resource [/WEB-INF/applicationContext.xml]; nested exception is java.lang.ClassCastException: weblogic.xml.jaxp.RegistryDocumentBuilderFactory cannot be cast to javax.xml.parsers.DocumentBuilderFactory at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:412) at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:334) at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:302) at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:174) at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:209) Truncated. see log file for complete stacktrace Caused By: java.lang.ClassCastException: weblogic.xml.jaxp.RegistryDocumentBuilderFactory cannot be cast to javax.xml.parsers.DocumentBuilderFactory at javax.xml.parsers.DocumentBuilderFactory.newInstance(Unknown Source) at org.springframework.beans.factory.xml.DefaultDocumentLoader.createDocumentBuilderFactory(DefaultDocumentLoader.java:89) at org.springframework.beans.factory.xml.DefaultDocumentLoader.loadDocument(DefaultDocumentLoader.java:70) at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:388) at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:334) Truncated. see log file for complete stacktrace > <Sep 20, 2016 11:21:40 AM EEST> <Warning> <HTTP> <BEA-101162> <User defined listener org.springframework.web.context.ContextLoaderListener failed: java.lang.IllegalStateException: BeanFactory not initialized or already closed - call 'refresh' before accessing beans via the ApplicationContext. java.lang.IllegalStateException: BeanFactory not initialized or already closed - call 'refresh' before accessing beans via the ApplicationContext at org.springframework.context.support.AbstractRefreshableApplicationContext.getBeanFactory(AbstractRefreshableApplicationContext.java:171) at org.springframework.context.support.AbstractApplicationContext.destroyBeans(AbstractApplicationContext.java:1090) at org.springframework.context.support.AbstractApplicationContext.doClose(AbstractApplicationContext.java:1064) at org.springframework.context.support.AbstractApplicationContext.close(AbstractApplicationContext.java:1010) at org.springframework.web.context.ContextLoader.closeWebApplicationContext(ContextLoader.java:559) Truncated. see log file for complete stacktrace > <Sep 20, 2016 11:21:40 AM EEST> <Error> <Deployer> <BEA-149231> <Unable to set the activation state to true for the application "nsi-client". weblogic.application.ModuleException: java.lang.ClassCastException: weblogic.xml.jaxp.RegistryDocumentBuilderFactory cannot be cast to javax.xml.parsers.DocumentBuilderFactory at weblogic.application.internal.ExtensibleModuleWrapper.start(ExtensibleModuleWrapper.java:140) at weblogic.application.internal.flow.ModuleListenerInvoker.start(ModuleListenerInvoker.java:124) at weblogic.application.internal.flow.ModuleStateDriver$3.next(ModuleStateDriver.java:216) at weblogic.application.internal.flow.ModuleStateDriver$3.next(ModuleStateDriver.java:211) at weblogic.application.utils.StateMachineDriver.nextState(StateMachineDriver.java:42) Truncated. see log file for complete stacktrace Caused By: java.lang.ClassCastException: weblogic.xml.jaxp.RegistryDocumentBuilderFactory cannot be cast to javax.xml.parsers.DocumentBuilderFactory at javax.xml.parsers.DocumentBuilderFactory.newInstance(Unknown Source) at org.springframework.beans.factory.xml.DefaultDocumentLoader.createDocumentBuilderFactory(DefaultDocumentLoader.java:89) at org.springframework.beans.factory.xml.DefaultDocumentLoader.loadDocument(DefaultDocumentLoader.java:70) at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:388) at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:334) Truncated. see log file for complete stacktrace >

在其他地方创建
nImage

,其格式不是TIFF,代码完成于:

var resized = new Bitmap((int)nW, (int)nH)

答案 1 :(得分:0)

您的新图片没有任何格式,当您创建新的Bitmap对象时,它不会自动从您的原始图片中读取,因此被写为jpg,(来自最终{{1}你的部分代码)

解决此问题的方法是将原始格式转换为else,并在将新图像写入imageToByteArray时使用该格式。您也可以取消长MemoryStream

if..else

您的主叫代码应为

public byte[] imageToByteArray(Image imageIn, ImageFormat format)
{
    using(var ms = new MemoryStream())
    {
        imageIn.Save(ms,format);
        return ms.ToArray();
    }
 }