Avatar billede hifi4all Nybegynder
16. maj 2010 - 15:13 Der er 2 kommentarer

Hjælp til resize - bedre kvalitet

Hej!

Jeg anvender nedenstående C# kode sammen med noget gammel klassisk ASP kode til resize af billeder, men da kvaliteten af scaleringen er ringe (og "qQuality" variablen i koden er ikke meget anvendelig), ville jeg høre om der var en venlig sjæl der kunne være behjælpelig med at få implementeret følgende funktoner:

CompositingMode = CompositingMode.SourceCopy;
CompositingQuality = CompositingQuality.HighQuality;
SmoothingMode = SmoothingMode.HighQuality;
InterpolationMode = InterpolationMode.HighQualityBicubic;

Har prøvet et hav af gange nu, desværre uden resultat! :(

På forhånd tak for hjælpen!


Kode:

<%@ Page Language="C#"%>
<%@ Import Namespace="System.Drawing" %>
<%@ Import Namespace="System.Drawing.Imaging" %>
<script runat="server">

    void Page_Load(Object s, EventArgs e) {
       
        int intNewWidth,intNewHeight, maxWidth = 800, maxHeight = 600, qQuality = 75;
    if ( Request["w"] != null) maxWidth = int.Parse(Request["w"]);
    if ( Request["h"] != null) maxHeight = int.Parse(Request["h"]);
    if ( Request["q"] != null) qQuality = int.Parse(Request["q"]);
       
        //get image from parameter
        string pictureFileName = Request["f"];
    string newFileName = Request["nf"];
    if (pictureFileName == null || pictureFileName == "" || !System.IO.File.Exists(pictureFileName)) {
      Response.Write("Error: File (" + pictureFileName + ") not found or empty"); 
      return;
    }
        System.Drawing.Image inputImage = System.Drawing.Image.FromFile(pictureFileName);
       
    //define size for new image
        string aspect = Request["a"];
        if (aspect == "true") {
            if (maxWidth < inputImage.Width || maxHeight < inputImage.Height) {
                if (maxWidth >= maxHeight) {
                    intNewWidth = (int)((double)maxHeight*((double)inputImage.Width/(double)inputImage.Height));
                    intNewHeight = maxHeight;
                } else {
                    intNewWidth = maxWidth;
                    intNewHeight = (int)((double)maxWidth*((double)inputImage.Height/(double)inputImage.Width));
                }
                if (intNewWidth > maxWidth) {
                    intNewWidth = maxWidth;
                    intNewHeight = (int)((double)maxWidth*((double)inputImage.Height/(double)inputImage.Width));
                }
                if (intNewHeight > maxHeight) {
                    intNewWidth = (int)((double)maxHeight*((double)inputImage.Width/(double)inputImage.Height));
                    intNewHeight = maxHeight;
                }
            } else {
                intNewWidth = inputImage.Width;
                intNewHeight = inputImage.Height;
            }
        } else {
                intNewWidth = maxWidth;
                intNewHeight = maxHeight;
        }

    try {       
      //output new image with different size
          Bitmap outputBitMap = new Bitmap(inputImage,intNewWidth,intNewHeight);
      inputImage.Dispose();

        EncoderParameters eps = new System.Drawing.Imaging.EncoderParameters(1);
        eps.Param[0] = new System.Drawing.Imaging.EncoderParameter( System.Drawing.Imaging.Encoder.Quality, qQuality );
       
        ImageCodecInfo ici = GetEncoderInfo("image/jpeg");
      if (pictureFileName.ToLower() == newFileName.ToLower())
        System.IO.File.Delete(pictureFileName);
      outputBitMap.Save(newFileName, ici, eps);
      outputBitMap.Dispose();     
    }       
    catch (Exception ex) {
        Response.Write("Error: " + ex);
            return;
    } 
   
    Response.Write(intNewWidth + ";" + intNewHeight + ";" + "DONE");
  }
   
  private static ImageCodecInfo GetEncoderInfo(String mimeType) {
    int j;
    ImageCodecInfo[] encoders;
    encoders = ImageCodecInfo.GetImageEncoders();
    for(j = 0; j < encoders.Length; ++j) {
      if(encoders[j].MimeType == mimeType)
        return encoders[j];
    }
    return null;
  }
   
</script>
Avatar billede hifi4all Nybegynder
19. maj 2010 - 11:52 #1
Ingen bud?
Avatar billede neess Nybegynder
30. maj 2010 - 10:54 #2
Jeg har benyttet mig af følgende kode, som har givet ok billede scallering

public static void MakeThumbnailFromStream(Stream sr, string destination, int widthPicture)
        {
            System.Drawing.Bitmap image = new System.Drawing.Bitmap(sr);

            int modulus = image.Width / widthPicture;
            int width = image.Width;
            int height = image.Height;
            //image.Dispose();
            //image = null;
            if (modulus > 0)
            {
                width = width / modulus;
                height = height / modulus;
            }
            System.Drawing.Image imageThumb = image.GetThumbnailImage(width, height, null, new IntPtr());
            EncoderParameter qualityParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, (long)10000);
            EncoderParameters encoderParams = new EncoderParameters(1);
            encoderParams.Param[0] = qualityParam;

            if (File.Exists(destination))
            {
                File.Delete(destination);
            }
            ImageCodecInfo ci = GetEncoder("image/jpeg");
            imageThumb.Save(destination, ci, encoderParams);
        }
        public static void MakeThumbnail(string picture, string destination, int widthPicture)
        {
            //System.Drawing.Image image = System.Drawing.Image.FromFile(picture);
            System.Drawing.Bitmap image = new System.Drawing.Bitmap(picture, false);

            int modulus = image.Width / widthPicture;
            int width = image.Width;
            int height = image.Height;
            //image.Dispose();
            //image = null;
            if (modulus > 0)
            {
                width = width / modulus;
                height = height / modulus;
            }
            System.Drawing.Image imageThumb = image.GetThumbnailImage(width, height, null, new IntPtr());
            EncoderParameter qualityParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, (long)100);

            EncoderParameters encoderParams = new EncoderParameters(1);
            encoderParams.Param[0] = qualityParam;

            if (File.Exists(destination))
            {
                File.Delete(destination);
            }
            ImageCodecInfo ci = GetEncoder("image/jpeg");
            imageThumb.Save(destination, ci, encoderParams);
        }
        public static ImageCodecInfo GetEncoder(string mimeType)
        {

            ImageCodecInfo[] encoders = ImageCodecInfo.GetImageDecoders();

            for (int i = 0; i < encoders.Length; ++i)

                if (encoders[i].MimeType == mimeType)

                    return encoders[i];

            return null;

        }
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester