2012-05-31 22 views
9

मैं छवि सहेजे गए छोटे आकार में रखना चाहता हूं। मैं इसका आकार कैसे बदल सकता हूं? मैं छवि redering के लिए इस कोड का उपयोग:बिटमैप छवि का आकार बदलें

Size size = new Size(surface.Width, surface.Height); 
surface.Measure(size); 
surface.Arrange(new Rect(size)); 
// Create a render bitmap and push the surface to it 
RenderTargetBitmap renderBitmap = 
    new RenderTargetBitmap(
     (int)size.Width, 
     (int)size.Height, 96d, 96d, 
     PixelFormats.Default); 
renderBitmap.Render(surface); 

BmpBitmapEncoder encoder = new BmpBitmapEncoder(); 
// push the rendered bitmap to it 
encoder.Frames.Add(BitmapFrame.Create(renderBitmap)); 
// save the data to the stream 
encoder.Save(outStream); 

उत्तर

3

अपने "सतह" करता है दृश्य क्षमता स्केलिंग है? यदि आप नहीं चाहते हैं तो आप इसे व्यूबॉक्स में लपेट सकते हैं, फिर इच्छित आकार पर व्यूबॉक्स प्रस्तुत करें।

जब आप सतह पर माप और व्यवस्थित करते हैं, तो आपको उस आकार को प्रदान करना चाहिए जिसे आप बिटमैप चाहते हैं।

Viewbox का उपयोग करने के लिए, की तरह कुछ करने के लिए अपने कोड बदलने के बाद:

Viewbox viewbox = new Viewbox(); 
Size desiredSize = new Size(surface.Width/2, surface.Height/2); 

viewbox.Child = surface; 
viewbox.Measure(desiredSize); 
viewbox.Arrange(new Rect(desiredSize)); 

RenderTargetBitmap renderBitmap = 
    new RenderTargetBitmap(
    (int)desiredSize.Width, 
    (int)desiredSize.Height, 96d, 96d, 
    PixelFormats.Default); 
renderBitmap.Render(viewbox); 
30
public static Bitmap ResizeImage(Bitmap imgToResize, Size size) 
{ 
    try 
    { 
     Bitmap b = new Bitmap(size.Width, size.Height); 
     using (Graphics g = Graphics.FromImage((Image)b)) 
     { 
      g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; 
      g.DrawImage(imgToResize, 0, 0, size.Width, size.Height); 
     } 
     return b; 
    } 
    catch 
    { 
     Console.WriteLine("Bitmap could not be resized"); 
     return imgToResize; 
    } 
} 
+1

यह कोशिश-पकड़ ब्लॉक के बिना बिल्कुल सही है। –

+0

आकार का हिस्सा महत्वपूर्ण है, मुझे 2 इंट्स का उपयोग करके कई पुराने उत्तर मिलते हैं लेकिन आपको अब आकार की आवश्यकता है। उस पर ध्यान देने के लिए धन्यवाद, कुछ परेशानी बचाई। (भविष्य में दर्शकों के लिए 2 इंट्स का उपयोग करके जवाब जानने के लिए टिप्पणी करने के लिए टिप्पणी करना) –

5

एक बिटमैप आकार बदलने के लिए सबसे छोटा रास्ता एक बिटमैप-निर्माता के लिए यह एक साथ पारित करने के लिए वांछित size (या width and height) के साथ है :

bitmap = new Bitmap(bitmap, width, height); 
+1

@Downvoter कृपया – Breeze

+0

मेरे लिए काम किया है। किसी के बुरे शिष्टाचार की भरपाई करने के लिए एक उत्थान है। – srking

+0

पूरी तरह से काम किया। – theMohammedA

संबंधित मुद्दे