कैसे एक छवि C # का आकार बदलने के लिए


288

के रूप में Size, Widthऔर Heightकर रहे हैं Get()के गुणों System.Drawing.Image;
मैं C # में रन-टाइम पर एक इमेज ऑब्जेक्ट का आकार कैसे बदल सकता हूं?

अभी, मैं एक नया Imageप्रयोग कर रहा हूँ :

// objImage is the original Image
Bitmap objBitmap = new Bitmap(objImage, new Size(227, 171));

2
सही तरीका नहीं है ... निम्न-गुणवत्ता वाले प्रक्षेप का उपयोग करता है और मूल धारा को नई बिटमैप छवि की अवधि के लिए बंद रहने का कारण बन सकता है ... अपनी स्वयं की छवि का समाधान करने से पहले चित्रण सूची का आकार बदलने वाली छवि पढ़ें
लिलिथ नदी

2
निस्तारण करो! उपयोग () {} काम करता है!
स्कॉट

8
यदि ये उत्तर सहायक हैं, तो स्वीकृत उत्तर को चिह्नित करने पर विचार करें।
जोएल

3
किसी भी अतिरिक्त पुस्तकालय का उपयोग करने की आवश्यकता नहीं है। मार्क द्वारा नीचे पोस्ट किया गया कोड पूरी तरह से काम करता है।
एल्म्यू

9
मार्क कौन है? मैं उसका उत्तर खोजने में विफल रहा, लेकिन 3 टिप्पणियाँ हैं जो इसका उल्लेख करती हैं।

जवाबों:


490

यह उच्च गुणवत्ता वाले आकार का प्रदर्शन करेगा:

/// <summary>
/// Resize the image to the specified width and height.
/// </summary>
/// <param name="image">The image to resize.</param>
/// <param name="width">The width to resize to.</param>
/// <param name="height">The height to resize to.</param>
/// <returns>The resized image.</returns>
public static Bitmap ResizeImage(Image image, int width, int height)
{
    var destRect = new Rectangle(0, 0, width, height);
    var destImage = new Bitmap(width, height);

    destImage.SetResolution(image.HorizontalResolution, image.VerticalResolution);

    using (var graphics = Graphics.FromImage(destImage))
    {
        graphics.CompositingMode = CompositingMode.SourceCopy;
        graphics.CompositingQuality = CompositingQuality.HighQuality;
        graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
        graphics.SmoothingMode = SmoothingMode.HighQuality;
        graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;

        using (var wrapMode = new ImageAttributes())
        {
            wrapMode.SetWrapMode(WrapMode.TileFlipXY);
            graphics.DrawImage(image, destRect, 0, 0, image.Width,image.Height, GraphicsUnit.Pixel, wrapMode);
        }
    }

    return destImage;
}
  • wrapMode.SetWrapMode(WrapMode.TileFlipXY) छवि सीमाओं के आसपास भूत को रोकता है - भोले का आकार बदलना छवि सीमाओं से परे पारदर्शी पिक्सल का नमूना देगा, लेकिन छवि को प्रतिबिंबित करके हम एक बेहतर नमूना प्राप्त कर सकते हैं (यह सेटिंग बहुत ध्यान देने योग्य है)
  • destImage.SetResolution भौतिक आकार की परवाह किए बिना DPI बनाए रखता है - छवि आयामों को कम करते समय या छपाई करते समय गुणवत्ता में वृद्धि हो सकती है
  • कंपोजिटिंग नियंत्रण यह बताता है कि पिक्सेल को पृष्ठभूमि के साथ कैसे मिश्रित किया जाता है - इसकी आवश्यकता नहीं हो सकती है क्योंकि हम केवल एक ही चीज़ खींच रहे हैं।
    • graphics.CompositingModeयह निर्धारित करता है कि स्रोत छवि से पिक्सेल अधिलेखित हैं या पृष्ठभूमि पिक्सेल के साथ संयुक्त हैं। SourceCopyनिर्दिष्ट करता है कि जब कोई रंग प्रदान किया जाता है, तो वह पृष्ठभूमि रंग को अधिलेखित कर देता है।
    • graphics.CompositingQuality स्तरित छवियों के प्रतिपादन गुणवत्ता स्तर को निर्धारित करता है।
  • graphics.InterpolationMode निर्धारित करता है कि दो समापन बिंदुओं के बीच मध्यवर्ती मान की गणना कैसे की जाती है
  • graphics.SmoothingMode निर्दिष्ट करता है कि क्या लाइनें, घटता है, और भरे हुए क्षेत्रों के किनारों को चौरसाई का उपयोग किया जाता है (जिसे एंटीएलियासिंग भी कहा जाता है) - शायद केवल डॉक्टरों पर काम करता है
  • graphics.PixelOffsetMode नई छवि बनाते समय गुणवत्ता प्रदान करने को प्रभावित करता है

पहलू अनुपात बनाए रखना पाठक के लिए एक अभ्यास के रूप में छोड़ दिया जाता है (वास्तव में, मुझे नहीं लगता कि यह आपके लिए यह कार्य करना है)।

इसके अलावा, यह एक अच्छा लेख है जिसमें छवि के आकार बदलने के कुछ नुकसान हैं। उपरोक्त फ़ंक्शन उनमें से अधिकांश को कवर करेगा, लेकिन आपको अभी भी बचत के बारे में चिंता करना होगा ।


4
कोड पूरी तरह से काम करता है जब छवि का आकार बदल जाता है लेकिन आकार 66KB से बढ़कर 132 KB हो जाता है। कुदाल मैं इसे कम कर सकता हूं
चामरा

3
@chamara शायद आपके द्वारा चुनी गई गुणवत्ता को बचाने के कारण है। देखें msdn.microsoft.com/en-us/library/bb882583(v=vs.110).aspx कोशिश गुणवत्ता = 90
mpen

3
@kstubs तुम्हें यकीन है कि कर रहे हैं। Bitmapअनिवार्य रूप से सिर्फ कक्षा का नाम है, आप इसे जो भी फ़ाइल प्रकार पसंद करते हैं उसे सहेज सकते हैं।
मपेन

5
@dotNetBlackBelt आपको संभवतः एक संदर्भ System.Drawingजोड़ने और जोड़ने की आवश्यकता हैusing System.Drawing.Imaging;
एमपीएन

2
यह मूल पहलू अनुपात को सही नहीं बनाए रखेगा?
कास्पर स्कोव

148

यह निश्चित नहीं है कि इस बारे में क्या मुश्किल है, जो आप कर रहे थे, वह करें, फिर से आकार की छवि बनाने के लिए अतिभारित बिटमैप निर्माता का उपयोग करें, केवल एक चीज जिसे आप याद कर रहे थे, वह छवि डेटा प्रकार में वापस डाली गई थी:

    public static Image resizeImage(Image imgToResize, Size size)
    {
       return (Image)(new Bitmap(imgToResize, size));
    }

    yourImage = resizeImage(yourImage, new Size(50,50));

2
yourImageनई छवि को निर्दिष्ट करने से पहले क्या आपको निपटाना नहीं चाहिए ?
निक शॉ

3
आप इसे मैन्युअल रूप से डिस्पोज़ कर सकते हैं या आप कचरा संग्रहकर्ता को यह काम करने दे सकते हैं। कोई बात नहीं।
एल्म्यू

23
यह कोड आकार बदलने की गुणवत्ता पर कोई नियंत्रण नहीं देता है जो बहुत महत्वपूर्ण है। मार्क से जवाब पर एक नजर है।
Elmue

43

में इस सवाल का , तुम मेरे सहित कुछ जवाब, होगा:

public Image resizeImage(int newWidth, int newHeight, string stPhotoPath)
 {
     Image imgPhoto = Image.FromFile(stPhotoPath); 

     int sourceWidth = imgPhoto.Width;
     int sourceHeight = imgPhoto.Height;

     //Consider vertical pics
    if (sourceWidth < sourceHeight)
    {
        int buff = newWidth;

        newWidth = newHeight;
        newHeight = buff;
    }

    int sourceX = 0, sourceY = 0, destX = 0, destY = 0;
    float nPercent = 0, nPercentW = 0, nPercentH = 0;

    nPercentW = ((float)newWidth / (float)sourceWidth);
    nPercentH = ((float)newHeight / (float)sourceHeight);
    if (nPercentH < nPercentW)
    {
        nPercent = nPercentH;
        destX = System.Convert.ToInt16((newWidth -
                  (sourceWidth * nPercent)) / 2);
    }
    else
    {
        nPercent = nPercentW;
        destY = System.Convert.ToInt16((newHeight -
                  (sourceHeight * nPercent)) / 2);
    }

    int destWidth = (int)(sourceWidth * nPercent);
    int destHeight = (int)(sourceHeight * nPercent);


    Bitmap bmPhoto = new Bitmap(newWidth, newHeight,
                  PixelFormat.Format24bppRgb);

    bmPhoto.SetResolution(imgPhoto.HorizontalResolution,
                 imgPhoto.VerticalResolution);

    Graphics grPhoto = Graphics.FromImage(bmPhoto);
    grPhoto.Clear(Color.Black);
    grPhoto.InterpolationMode =
        System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;

    grPhoto.DrawImage(imgPhoto,
        new Rectangle(destX, destY, destWidth, destHeight),
        new Rectangle(sourceX, sourceY, sourceWidth, sourceHeight),
        GraphicsUnit.Pixel);

    grPhoto.Dispose();
    imgPhoto.Dispose();
    return bmPhoto;
}

5
आप imgPhoto.Dispose () भूल गए; फ़ाइल को उपयोग में रखा गया है
shrutyzet

1
यह बहुत उपयोगी है, और मैं अपने ऐप में इसका उपयोग कर रहा हूं। हालाँकि, यह नोट करना महत्वपूर्ण है कि यह एल्गोरिथम पारदर्शी कल्पनाओं के साथ काम नहीं करता है .. यह सभी पारदर्शी पिक्सेल को काला कर देता है। शायद यह तय करना आसान है, लेकिन उपयोगकर्ताओं के लिए यह सिर्फ एक नोट है। :)
मेम

1
क्या आप छवि को बचाने के लिए नहीं हैं? imgPhoto.Save ()?
व्हिपलैश

@ मीम क्या आप पारदर्शी डॉक के लिए उस काली पृष्ठभूमि को ठीक करने के लिए लिंक दे सकते हैं।
सैयद मोहम्मद

25

System.Drawing.Image.GetThumbnailImageविधि का उपयोग क्यों नहीं करते ?

public Image GetThumbnailImage(
    int thumbWidth, 
    int thumbHeight, 
    Image.GetThumbnailImageAbort callback, 
    IntPtr callbackData)

उदाहरण:

Image originalImage = System.Drawing.Image.FromStream(inputStream, true, true);
Image resizedImage = originalImage.GetThumbnailImage(newWidth, (newWidth * originalImage.Height) / originalWidth, null, IntPtr.Zero);
resizedImage.Save(imagePath, ImageFormat.Png);

स्रोत: http://msdn.microsoft.com/en-us/library/system.drawing.image.getthumbnailimage.aspx


6
यह एक छवि का आकार बदलने का सही तरीका नहीं है। यदि यह मौजूद है तो यह jpg से थंबनेल खींचता है। यदि यह मौजूद नहीं है, तो आपके पास गुणवत्ता या नई छवि पर कोई नियंत्रण नहीं है। इसके अलावा, इस कोड में मेमोरी लीक है।
रॉबर्ट स्मिथ

1
@Busrot क्यों यह स्मृति लीक का कारण होगा?
उपयोगकर्ता

2
GDI लाइब्रेरी में कुछ भी अभी भी अप्रबंधित चल रहा है। उपयोग किए गए कथन का उपयोग किए बिना या बाद में वस्तुओं के निपटान के बिना, सिस्टम को उन वस्तुओं को इकट्ठा करने और फिर से स्मृति उपलब्ध कराने में लंबा समय लग सकता है।
रॉबर्ट स्मिथ

9
जैसा कि आप कहते हैं: इसमें लंबा समय लग सकता है। लेकिन यह मेमोरी लीक नहीं है। यदि स्मृति को मुक्त किया जाएगा तो यह स्मृति रिसाव होगा। लेकिन यह कचरा कलेक्टर का सामान्य व्यवहार है कि यह सीपीयू के निष्क्रिय होने पर स्मृति को मुक्त करता है। उपयोग () कथन स्मृति लीक को नहीं रोकता है। यह सिर्फ मेमोरी को तुरंत मुक्त करता है जबकि कचरा संग्रहकर्ता उस समय मेमोरी को मुक्त करता है जब उसके पास ऐसा करने का समय होता है। इस विशिष्ट मामले में यही अंतर है।
एल्म्यू

छवि के आकार बदलने के नुकसान देखें: nathanaeljones.com/blog/2009/20-image-resizing-pitashes "GetThumbnailImage () का उपयोग कर। GetThumbnailImage () स्पष्ट विकल्प लगता है, और कई लेख इसके उपयोग की सलाह देते हैं। दुर्भाग्य से, यह हमेशा एम्बेडेड jpeg को पकड़ लेता है। थंबनेल अगर मौजूद है। कुछ तस्वीरों में ये हैं, कुछ नहीं है - यह आमतौर पर आपके कैमरे पर निर्भर करता है। आपको आश्चर्य होगा कि क्यों GetThumbnailImage कुछ फोटो पर अच्छा काम करता है, लेकिन दूसरों पर बुरी तरह से धुंधला हो जाता है। GetThumbnailImage () फ़ोटो के लिए विश्वसनीय नहीं है। 10px से 10px उस कारण से। "
निक पेंटर

12

आप net-vips , libvips के लिए C # बाइंडिंग आज़मा सकते हैं । यह एक आलसी, स्ट्रीमिंग, मांग-संचालित छवि प्रसंस्करण पुस्तकालय है, इसलिए यह पूरी छवि को लोड करने की आवश्यकता के बिना इस तरह के संचालन कर सकता है।

उदाहरण के लिए, यह एक आसान छवि थंबनेलर के साथ आता है:

Image image = Image.Thumbnail("image.jpg", 300, 300);
image.WriteToFile("my-thumbnail.jpg");

यह स्मार्ट फसल का भी समर्थन करता है, जो बुद्धिमानी से छवि के सबसे महत्वपूर्ण भाग को निर्धारित करता है और छवि को क्रॉप करते समय ध्यान में रखता है। उदाहरण के लिए:

Image image = Image.Thumbnail("owl.jpg", 128, crop: "attention");
image.WriteToFile("tn_owl.jpg");

owl.jpgएक ऑफ-सेंटर रचना कहां है:

उल्लू

यह परिणाम देता है:

उल्लू की स्मार्ट फसल

पहले यह ऊर्ध्वाधर अक्ष को 128 पिक्सेल पर लाने के लिए छवि को सिकोड़ता है, फिर attentionरणनीति का उपयोग करते हुए 128 पिक्सेल तक फसल करता है। यह उन विशेषताओं के लिए छवि की खोज करता है जो एक मानव आंख को पकड़ सकती हैं Smartcrop(), विवरण के लिए देख सकते हैं ।


Libvips के लिए आपका बंधन बहुत अच्छा लगता है। मैं आपके परिवाद पर एक नजर जरूर डालूंगा। C # डेवलपर को उपलब्ध कराने के लिए धन्यवाद!
फ्रेंचटैस्टिक

यह शानदार है! मुझे नहीं पता था कि एक इमेज प्रोसेसिंग लाइब्रेरी यह अच्छी दिख सकती है।
दलवीर

अच्छा! ImageMagick भारी से बेहतर
मोशे एल

10

यह करेगा -

  • लूप की आवश्यकता के बिना चौड़ाई और ऊंचाई का आकार बदलें
  • छवियों के मूल आयामों से अधिक नहीं है

//////////////

private void ResizeImage(Image img, double maxWidth, double maxHeight)
{
    double resizeWidth = img.Source.Width;
    double resizeHeight = img.Source.Height;

    double aspect = resizeWidth / resizeHeight;

    if (resizeWidth > maxWidth)
    {
        resizeWidth = maxWidth;
        resizeHeight = resizeWidth / aspect;
    }
    if (resizeHeight > maxHeight)
    {
        aspect = resizeWidth / resizeHeight;
        resizeHeight = maxHeight;
        resizeWidth = resizeHeight * aspect;
    }

    img.Width = resizeWidth;
    img.Height = resizeHeight;
}

11
ओपी System.Drawing.Image के बारे में पूछ रहा था, जहां आपका कोड 'चौड़ाई ’और not ऊंचाई’ के रूप में काम नहीं करेगा। हालाँकि, यह System.Windows.Controls.Image के लिए काम करेगा।
mmmdreg

10
public static Image resizeImage(Image image, int new_height, int new_width)
{
    Bitmap new_image = new Bitmap(new_width, new_height);
    Graphics g = Graphics.FromImage((Image)new_image );
    g.InterpolationMode = InterpolationMode.High;
    g.DrawImage(image, 0, 0, new_width, new_height);
    return new_image;
}

आप ग्राफिक्स निपटाना भूल गए। बेहतर प्रक्षेप मोड के साथ नए बिटमैप (छवि, चौड़ाई, ऊंचाई) के रूप में एक ही सिद्धांत लगता है। मैं उत्सुक हूं कि डिफ़ॉल्ट क्या है ? क्या यह इससे भी बदतर है Low?
सिनत्र

9

यह कोड उपरोक्त उत्तरों में से एक के रूप में पोस्ट किया गया है .. लेकिन पारदर्शी पिक्सेल को काले के बजाय सफेद में परिवर्तित कर देगा ... धन्यवाद :)

    public Image resizeImage(int newWidth, int newHeight, string stPhotoPath)
    {
        Image imgPhoto = Image.FromFile(stPhotoPath);

        int sourceWidth = imgPhoto.Width;
        int sourceHeight = imgPhoto.Height;

        //Consider vertical pics
        if (sourceWidth < sourceHeight)
        {
            int buff = newWidth;

            newWidth = newHeight;
            newHeight = buff;
        }

        int sourceX = 0, sourceY = 0, destX = 0, destY = 0;
        float nPercent = 0, nPercentW = 0, nPercentH = 0;

        nPercentW = ((float)newWidth / (float)sourceWidth);
        nPercentH = ((float)newHeight / (float)sourceHeight);
        if (nPercentH < nPercentW)
        {
            nPercent = nPercentH;
            destX = System.Convert.ToInt16((newWidth -
                      (sourceWidth * nPercent)) / 2);
        }
        else
        {
            nPercent = nPercentW;
            destY = System.Convert.ToInt16((newHeight -
                      (sourceHeight * nPercent)) / 2);
        }

        int destWidth = (int)(sourceWidth * nPercent);
        int destHeight = (int)(sourceHeight * nPercent);


        Bitmap bmPhoto = new Bitmap(newWidth, newHeight,
                      PixelFormat.Format24bppRgb);

        bmPhoto.SetResolution(imgPhoto.HorizontalResolution,
                     imgPhoto.VerticalResolution);

        Graphics grPhoto = Graphics.FromImage(bmPhoto);
        grPhoto.Clear(Color.White);
        grPhoto.InterpolationMode =
            System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;

        grPhoto.DrawImage(imgPhoto,
            new Rectangle(destX, destY, destWidth, destHeight),
            new Rectangle(sourceX, sourceY, sourceWidth, sourceHeight),
            GraphicsUnit.Pixel);

        grPhoto.Dispose();
        imgPhoto.Dispose();

        return bmPhoto;
    }

7

मैंने जो आवेदन किया उसमें कई विकल्पों के साथ एक फ़ंक्शन बनाना आवश्यक था। यह काफी बड़ा है, लेकिन यह छवि का आकार बदलता है, पहलू अनुपात रख सकता है और छवि के केवल केंद्र को वापस करने के लिए किनारों को काट सकता है:

/// <summary>
    /// Resize image with a directory as source
    /// </summary>
    /// <param name="OriginalFileLocation">Image location</param>
    /// <param name="heigth">new height</param>
    /// <param name="width">new width</param>
    /// <param name="keepAspectRatio">keep the aspect ratio</param>
    /// <param name="getCenter">return the center bit of the image</param>
    /// <returns>image with new dimentions</returns>
    public Image resizeImageFromFile(String OriginalFileLocation, int heigth, int width, Boolean keepAspectRatio, Boolean getCenter)
    {
        int newheigth = heigth;
        System.Drawing.Image FullsizeImage = System.Drawing.Image.FromFile(OriginalFileLocation);

        // Prevent using images internal thumbnail
        FullsizeImage.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);
        FullsizeImage.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);

        if (keepAspectRatio || getCenter)
        {
            int bmpY = 0;
            double resize = (double)FullsizeImage.Width / (double)width;//get the resize vector
            if (getCenter)
            {
                bmpY = (int)((FullsizeImage.Height - (heigth * resize)) / 2);// gives the Y value of the part that will be cut off, to show only the part in the center
                Rectangle section = new Rectangle(new Point(0, bmpY), new Size(FullsizeImage.Width, (int)(heigth * resize)));// create the section to cut of the original image
                //System.Console.WriteLine("the section that will be cut off: " + section.Size.ToString() + " the Y value is minimized by: " + bmpY);
                Bitmap orImg = new Bitmap((Bitmap)FullsizeImage);//for the correct effect convert image to bitmap.
                FullsizeImage.Dispose();//clear the original image
                using (Bitmap tempImg = new Bitmap(section.Width, section.Height))
                {
                    Graphics cutImg = Graphics.FromImage(tempImg);//              set the file to save the new image to.
                    cutImg.DrawImage(orImg, 0, 0, section, GraphicsUnit.Pixel);// cut the image and save it to tempImg
                    FullsizeImage = tempImg;//save the tempImg as FullsizeImage for resizing later
                    orImg.Dispose();
                    cutImg.Dispose();
                    return FullsizeImage.GetThumbnailImage(width, heigth, null, IntPtr.Zero);
                }
            }
            else newheigth = (int)(FullsizeImage.Height / resize);//  set the new heigth of the current image
        }//return the image resized to the given heigth and width
        return FullsizeImage.GetThumbnailImage(width, newheigth, null, IntPtr.Zero);
    }

फ़ंक्शन को आसान बनाने के लिए कुछ अतिभारित कार्यों को जोड़ना संभव है:

/// <summary>
    /// Resize image with a directory as source
    /// </summary>
    /// <param name="OriginalFileLocation">Image location</param>
    /// <param name="heigth">new height</param>
    /// <param name="width">new width</param>
    /// <returns>image with new dimentions</returns>
    public Image resizeImageFromFile(String OriginalFileLocation, int heigth, int width)
    {
        return resizeImageFromFile(OriginalFileLocation, heigth, width, false, false);
    }

    /// <summary>
    /// Resize image with a directory as source
    /// </summary>
    /// <param name="OriginalFileLocation">Image location</param>
    /// <param name="heigth">new height</param>
    /// <param name="width">new width</param>
    /// <param name="keepAspectRatio">keep the aspect ratio</param>
    /// <returns>image with new dimentions</returns>
    public Image resizeImageFromFile(String OriginalFileLocation, int heigth, int width, Boolean keepAspectRatio)
    {
        return resizeImageFromFile(OriginalFileLocation, heigth, width, keepAspectRatio, false);
    }

अब अंतिम दो बूलियन सेट करने के लिए वैकल्पिक हैं। फ़ंक्शन को इस तरह से कॉल करें:

System.Drawing.Image ResizedImage = resizeImageFromFile(imageLocation, 800, 400, true, true);

6
public string CreateThumbnail(int maxWidth, int maxHeight, string path)
{

    var image = System.Drawing.Image.FromFile(path);
    var ratioX = (double)maxWidth / image.Width;
    var ratioY = (double)maxHeight / image.Height;
    var ratio = Math.Min(ratioX, ratioY);
    var newWidth = (int)(image.Width * ratio);
    var newHeight = (int)(image.Height * ratio);
    var newImage = new Bitmap(newWidth, newHeight);
    Graphics thumbGraph = Graphics.FromImage(newImage);

    thumbGraph.CompositingQuality = CompositingQuality.HighQuality;
    thumbGraph.SmoothingMode = SmoothingMode.HighQuality;
    //thumbGraph.InterpolationMode = InterpolationMode.HighQualityBicubic;

    thumbGraph.DrawImage(image, 0, 0, newWidth, newHeight);
    image.Dispose();

    string fileRelativePath = "newsizeimages/" + maxWidth + Path.GetFileName(path);
    newImage.Save(Server.MapPath(fileRelativePath), newImage.RawFormat);
    return fileRelativePath;
}

यहां क्लिक करें http://bhupendrasinghsaini.blogspot.in/2014/07/resize-image-in-c.html


6

यह वह कोड है जिसे मैंने एक विशिष्ट आवश्यकता के लिए काम किया है: गंतव्य हमेशा परिदृश्य अनुपात में होता है। यह आपको एक अच्छी शुरुआत देनी चाहिए।

public Image ResizeImage(Image source, RectangleF destinationBounds)
{
    RectangleF sourceBounds = new RectangleF(0.0f,0.0f,(float)source.Width, (float)source.Height);
    RectangleF scaleBounds = new RectangleF();

    Image destinationImage = new Bitmap((int)destinationBounds.Width, (int)destinationBounds.Height);
    Graphics graph = Graphics.FromImage(destinationImage);
    graph.InterpolationMode =
        System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;

    // Fill with background color
    graph.FillRectangle(new SolidBrush(System.Drawing.Color.White), destinationBounds);

    float resizeRatio, sourceRatio;
    float scaleWidth, scaleHeight;

    sourceRatio = (float)source.Width / (float)source.Height;

    if (sourceRatio >= 1.0f)
    {
        //landscape
        resizeRatio = destinationBounds.Width / sourceBounds.Width;
        scaleWidth = destinationBounds.Width;
        scaleHeight = sourceBounds.Height * resizeRatio;
        float trimValue = destinationBounds.Height - scaleHeight;
        graph.DrawImage(source, 0, (trimValue / 2), destinationBounds.Width, scaleHeight);
    }
    else
    {
        //portrait
        resizeRatio = destinationBounds.Height/sourceBounds.Height;
        scaleWidth = sourceBounds.Width * resizeRatio;
        scaleHeight = destinationBounds.Height;
        float trimValue = destinationBounds.Width - scaleWidth;
        graph.DrawImage(source, (trimValue / 2), 0, scaleWidth, destinationBounds.Height);
    }

    return destinationImage;

}

बहुत बढ़िया!!! मुझे पोर्ट्रेट इमेज में दिक्कत थी और वेब पर कई समाधानों को आजमाने के बाद, यह केवल सही था! आपको बहुत - बहुत धन्यवाद!
फैबियो

3

यदि आप इसके साथ काम कर रहे हैं BitmapSource:

var resizedBitmap = new TransformedBitmap(
    bitmapSource,
    new ScaleTransform(scaleX, scaleY));

यदि आप गुणवत्ता पर बेहतर नियंत्रण चाहते हैं, तो इसे पहले चलाएं:

RenderOptions.SetBitmapScalingMode(
    bitmapSource,
    BitmapScalingMode.HighQuality);

(डिफ़ॉल्ट BitmapScalingMode.Linearजो के बराबर है BitmapScalingMode.LowQuality।)


3

मैं ImageProcessorCore का उपयोग करता हूं, ज्यादातर क्योंकि यह काम करता है। नेट कोर।

और इसके पास अधिक विकल्प हैं जैसे कि प्रकार को परिवर्तित करना, छवियों को क्रॉप करना और अधिक

http://imageprocessor.org/imageprocessor/


1
मैंने देखा और यह .NET कोर का समर्थन नहीं करता है। यह पूर्ण ढांचे के खिलाफ बनाया गया है।

1

आकार बदलने और एक छवि को आनुपातिक रखते हुए कैनवास की तरह ऊँचाई के नीचे फिट करने के लिए सहेजें

using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;

namespace Infra.Files
{
    public static class GenerateThumb
    {
        /// <summary>
        /// Resize and save an image to fit under width and height like a canvas keeping things proportional
        /// </summary>
        /// <param name="originalImagePath"></param>
        /// <param name="thumbImagePath"></param>
        /// <param name="newWidth"></param>
        /// <param name="newHeight"></param>
        public static void GenerateThumbImage(string originalImagePath, string thumbImagePath, int newWidth, int newHeight)
        {
            Bitmap srcBmp = new Bitmap(originalImagePath);
            float ratio = 1;
            float minSize = Math.Min(newHeight, newHeight);

            if (srcBmp.Width > srcBmp.Height)
            {
                ratio = minSize / (float)srcBmp.Width;
            }
            else
            {
                ratio = minSize / (float)srcBmp.Height;
            }

            SizeF newSize = new SizeF(srcBmp.Width * ratio, srcBmp.Height * ratio);
            Bitmap target = new Bitmap((int)newSize.Width, (int)newSize.Height);

            using (Graphics graphics = Graphics.FromImage(target))
            {
                graphics.CompositingQuality = CompositingQuality.HighSpeed;
                graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
                graphics.CompositingMode = CompositingMode.SourceCopy;
                graphics.DrawImage(srcBmp, 0, 0, newSize.Width, newSize.Height);

                using (MemoryStream memoryStream = new MemoryStream())
                {
                    target.Save(thumbImagePath);
                }
            }
        }
    }
}

1

छवि आकार बदलने के लिए नीचे दिए गए उदाहरण के साथ फ़ंक्शन का उपयोग करें:

//Example : 
System.Net.Mime.MediaTypeNames.Image newImage = System.Net.Mime.MediaTypeNames.Image.FromFile("SampImag.jpg");
System.Net.Mime.MediaTypeNames.Image temImag = FormatImage(newImage, 100, 100);

//image size modification unction   
public static System.Net.Mime.MediaTypeNames.Image FormatImage(System.Net.Mime.MediaTypeNames.Image img, int outputWidth, int outputHeight)
{

    Bitmap outputImage = null;
    Graphics graphics = null;
    try
    {
         outputImage = new Bitmap(outputWidth, outputHeight, System.Drawing.Imaging.PixelFormat.Format16bppRgb555);
         graphics = Graphics.FromImage(outputImage);
         graphics.DrawImage(img, new Rectangle(0, 0, outputWidth, outputHeight),
         new Rectangle(0, 0, img.Width, img.Height), GraphicsUnit.Pixel);

         return outputImage;
     }
     catch (Exception ex)
     {
           return img;
     }
}

2
कृपया इस कोड का उपयोग कैसे करें, कोड क्या करता है, और यह मूल प्रश्न में समस्या को हल कैसे करता है, के ऊपर अपने उत्तर में समझाने पर विचार करें।
टिम विज़े

मैंने उपयोग का मामला भी जोड़ा है। ऊपर फ़ंक्शन का उपयोग करें bellow उदाहरण। छवि newImage = Image.FromFile ("SampImag.jpg"); छवि टेम्पमैग = फॉर्मेटमैज (newImage, 100, 100);
प्रसाद केएम


0

नोट: यह ASP.Net कोर के साथ काम नहीं करेगा क्योंकि WebImage System.Web पर निर्भर करता है, लेकिन ASP.Net के पिछले संस्करणों पर मैंने कई बार इस स्निपेट का उपयोग किया और उपयोगी था।

String ThumbfullPath = Path.GetFileNameWithoutExtension(file.FileName) + "80x80.jpg";
var ThumbfullPath2 = Path.Combine(ThumbfullPath, fileThumb);
using (MemoryStream stream = new MemoryStream(System.IO.File.ReadAllBytes(fullPath)))
{
      var thumbnail = new WebImage(stream).Resize(80, 80);
      thumbnail.Save(ThumbfullPath2, "jpg");
}
हमारी साइट का प्रयोग करके, आप स्वीकार करते हैं कि आपने हमारी Cookie Policy और निजता नीति को पढ़ और समझा लिया है।
Licensed under cc by-sa 3.0 with attribution required.