2009-01-28 177 views
1

我想,我目前使用从网站根目录下存放的文件夹中的图像文件的缩略图内置在.NET capebilites动态生成缩略图,但质量变得相当糟糕,并genererate因为它是一个网上商店,这是一个真正的问题我的问题是,如果有任何好的(开源?)框架,可以帮助我处理图像缩略图创建和rezise,并可能帮助图像文件大小压缩上传过程中?图像缩略图ASP.NET

我看了,但还没有找到任何呢?

在此先感谢。

+0

http://imageresizing.net - 它是免费的,开源,支持(由我),并避免了[29图像缩放陷阱(http://nathanaeljones.com/163/20-image-resizing-每个人都有的陷阱/)。 – 2011-06-21 23:54:48

回答

4

我写了一些你可以使用的代码。请不要卖掉它;)

这是代码:

/* 
* Software Developed by Filip Ekberg ([email protected]) 
* 
* For Questions regarding this software, please send me an E-mail 
* 
*/ 

#region Usings 
using System; 
using System.Collections.Generic; 
using System.Text; 
using System.Drawing; 
using System.Drawing.Drawing2D; 
using System.Drawing.Imaging; 
#endregion 

namespace ImageResizer 
{ 
    class ImageProcessor 
    { 
     #region Members 
     private int m_imgWidht, 
        m_imgHeight, 
        m_oImgWidht, 
        m_oImgHeight; 

     private float m_vertRes, 
         m_horiRes; 

     private double m_size = 0.25; 

     private Bitmap m_Bitmap; 
     private Graphics m_Graphics; 
     private Image m_currentImage; 

     private PixelFormat m_pxlFormat = PixelFormat.Format32bppRgb; 
     private InterpolationMode m_interpolationMode = InterpolationMode.HighQualityBilinear; 
     private CompositingQuality m_compositingQuality = CompositingQuality.HighQuality; 
     private SmoothingMode m_smoothingMode = SmoothingMode.None; 
     #endregion 

     #region Access Methods 

     public int ImageWidth 
     { 
      get { return m_imgWidht; } 
     } 
     public int ImageHeight 
     { 
      get { return m_imgHeight; } 
     } 
     public float VerticalResolution 
     { 
      get { return m_vertRes; } 
      set { m_vertRes = value; } 
     } 
     public float HorizonalResolution 
     { 
      get { return m_horiRes; } 
      set { m_horiRes = value; } 
     } 

     /// <summary> 
     /// Use ImageSize struct for setting this. 
     /// </summary> 
     public double Size 
     { 
      get { return m_size; } 
      set { m_size = value; } 
     } 
     public Image ProcessImage 
     { 
      get { return m_currentImage; } 
      set { m_currentImage = value; } 
     } 
     public PixelFormat ImagePixelFormat 
     { 
      get { return m_pxlFormat; } 
      set { m_pxlFormat = value; } 
     } 
     public InterpolationMode ImageInterpolationMode 
     { 
      get { return m_interpolationMode; } 
      set { m_interpolationMode = value; } 
     } 
     public CompositingQuality ImageCompositingQuality 
     { 
      get { return m_compositingQuality; } 
      set { m_compositingQuality = value; } 
     } 
     public SmoothingMode ImageSmoothingMode 
     { 
      get { return m_smoothingMode; } 
      set { m_smoothingMode = value; } 
     } 

     #endregion 

     #region Process Image 
     public Bitmap BeginProcess() 
     { 
      if (m_currentImage == null) 
       return null; 

      m_oImgWidht = m_currentImage.Width; 
      m_oImgHeight = m_currentImage.Height; 

      m_vertRes = m_currentImage.VerticalResolution; 
      m_horiRes = m_currentImage.HorizontalResolution; 

      m_imgWidht = (int)(m_oImgWidht * (double)m_size); 
      m_imgHeight = (int)(m_oImgHeight * (double)m_size); 


      m_Bitmap = new Bitmap(m_imgWidht, m_imgHeight, m_pxlFormat); 

      m_Bitmap.SetResolution(m_vertRes, m_horiRes); 

      m_Graphics = Graphics.FromImage(m_Bitmap); 

      m_Graphics.InterpolationMode = m_interpolationMode; 

      m_Graphics.CompositingQuality = m_compositingQuality; 

      m_Graphics.SmoothingMode = m_smoothingMode; 

      // m_Graphics.DrawImage(m_currentImage, new Rectangle(0, 0, m_imgWidht, m_imgHeight), new Rectangle(0, 0, m_oImgWidht, m_oImgHeight), GraphicsUnit.Pixel); 

      Rectangle rectDestination = new Rectangle(0, 0, m_imgWidht, m_imgHeight); 
      // m_Graphics.DrawImage(m_currentImage, 
      //  new Rectangle(-1, -1, m_oImgWidht + 2, m_oImgHeight + 2), 
      // new Rectangle(m_oImgWidht, m_oImgHeight, m_imgWidht, m_oImgHeight), 
      // GraphicsUnit.Pixel); 
      ImageAttributes ia = new ImageAttributes(); 
      ia.SetWrapMode(WrapMode.TileFlipXY); 
      m_Graphics.DrawImage(m_currentImage, rectDestination, 0, 0, m_oImgWidht, m_oImgHeight, GraphicsUnit.Pixel, ia); 

      // m_Graphics.Dispose(); 

      return m_Bitmap; 
     } 
     #endregion 
    } 
} 

这是如何使用它:

private ImageProcessor m_processor = new ImageProcessor(); 


m_currentImage = new Bitmap(currentFile.FullName); 

       // Display a Thumbnail of it 
       pictureBox1.Image = m_currentImage.GetThumbnailImage(pictureBox1.Width, pictureBox1.Height, null, IntPtr.Zero); 

       // Update the Label with Filename and what file is currently processed 
       delegate_lblControl(currentFile.Name + "\n" + m_currentProcessedImage + "/" + m_fileList.Length); 

       // Increase the value on the ProcessBar 
       delegate_imageProcessValue(1); 

       // Process the Image 
       m_processor.ProcessImage = m_currentImage; 

       m_processor.Size = m_size; 

       m_finalProcessedImage = m_processor.BeginProcess(); 

       ImageCodecInfo jgpEncoder = GetEncoder(ImageFormat.Jpeg); 
       System.Drawing.Imaging.Encoder myEncoder = 
        System.Drawing.Imaging.Encoder.Quality; 

       EncoderParameters myEncoderParameters = new EncoderParameters(1); 
       EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, m_jpegQuality); 
       myEncoderParameters.Param[0] = myEncoderParameter; 


       // Save the Image to the Output folder 
       m_finalProcessedImage.Save(fldOutput.SelectedPath + "\\" + currentFile.Name, jgpEncoder, myEncoderParameters); 

       // Dispose the Images 
       m_finalProcessedImage.Dispose(); 

       m_currentImage.Dispose(); 

我所实际写入能做代码调整您的图像到特定的百分比。