2010-12-31 12 views
5

में स्टोर करें मेरे पास एक WPF एप्लिकेशन है जिसमें छवियों का एक सूची बॉक्स है। अभी मैं छवियों को लोड करने के लिए बिटमैप छवि और बिटमैप कैशऑप्शन.ऑनलोड का उपयोग कर रहा हूं। समस्या यह है कि जब बहुत सारी छवियां होती हैं, तो छवियों के आकार के कारण रैम का उपयोग आसमान रॉकेट होता है। मैं सूची बॉक्स में प्रदर्शित करने के लिए मूल की थंबनेल छवियों को कैसे बना सकता हूं? इसे शायद कैश किया जाना चाहिए क्योंकि जब मैं एप्लिकेशन चला रहा हूं तो निर्देशिका में छवि फ़ाइलों को हटाया जा सकता है या संशोधित किया जा सकता है।छवियों के थंबनेल बनाएं और उन्हें कैश

उत्तर

2

आप सभ्य अंगूठे बिटमैप्स InterpolationMode.HighQualityBicubic

Bitmap bitmap = ... 
    Bitmap thumbBitmap = new System.Drawing.Bitmap(thumbWidth, thumbHeight); 
    using (Graphics g = Graphics.FromImage(thumbBitmap)) 
    { 
     g.InterpolationMode = InterpolationMode.HighQualityBicubic; 
     g.DrawImage(bitmap, 0, 0, thumbWidth, thumbHeight); 
    } 

का उपयोग कर आप एक पृष्ठभूमि सूत्र में अंगूठे का निर्माण कर रहे हैं, बस उन्हें एक स्मृति धारा है जिसे आप lazily BitmapImage बनाने के लिए उपयोग कर सकते हैं में बचाने जब अनुरोध बना सकते हैं:

_ms = new MemoryStream(); 
    thumbBitmap.Save(_ms, ImageFormat.Png); 
    _ms.Position = 0; 
    ImageLoaded = true; 


    //thumb image property of this class, use in binding 
    public BitmapImage ThumbImage 
    { 
     get 
     { 
      if (_thumbImage == null && ImageLoaded) 
      { 
       BitmapImage bi = new BitmapImage(); 
       bi.BeginInit(); 
       bi.StreamSource = _ms; 
       bi.EndInit(); 
       _thumbImage = bi; 
      } 
      return _thumbImage; 
     } 
    } 
0

एमएसडीएन की जांच के बाद मुझे ऐसा करने का कोई सीधा तरीका नहीं दिख रहा है, लेकिन शायद आप छवि को हर इतने सारे पिक्सल का नमूना दे सकते हैं और डेटा को एक नई छवि में लिख सकते हैं, और फिर पूर्ण आकार को साफ कर सकते हैं।

2

यहां एक ऐसी विधि है जिसे मैंने बहुत पहले नहीं लिखा था जो आपकी मदद कर सकता है।

byte[] IImageResizer.CreateThumbnailBytes(byte[] originalImage) 
    { 
     Image thumbnail = null; 

     Image tempImage = Image.FromStream(new MemoryStream(originalImage)); 

     int desiredWidth = 160; 

     int newPixelWidth = tempImage.Width; 
     int newPixelHeight = tempImage.Height; 

     if (newPixelWidth > desiredWidth) 
     { 
      float resizePercent = ((float)desiredWidth/(float)tempImage.Width); 

      newPixelWidth = (int)(tempImage.Width * resizePercent) + 1; 
      newPixelHeight = (int)(tempImage.Height * resizePercent) + 1; 
     } 

     Bitmap bitmap = new Bitmap(newPixelWidth, newPixelHeight); 

     using (Graphics graphics = Graphics.FromImage((Image)bitmap)) 
     { 
      graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; 
      graphics.DrawImage(tempImage, 0, 0, newPixelWidth, newPixelHeight); 
     } 

     thumbnail = (Image)bitmap; 

     MemoryStream ms = new MemoryStream(); 
     thumbnail.Save(ms, ImageFormat.Jpeg); 

     return ms.ToArray(); 
    } 

मैं मूल छवियों में बाइनरी पास करता हूं, और लगभग 160px के आसपास छवि का आकार बदलता हूं।

उम्मीद है कि यह मदद करता है!

4

समस्या यह है कि जब बहुत सारी छवियां होती हैं, तो छवियों के आकार के कारण रैम का उपयोग आकाश रॉकेट होता है।

सी # उदाहरण से: http://msdn.microsoft.com/en-us/library/ms748873.aspx

// Create source 
BitmapImage myBitmapImage = new BitmapImage(); 

// BitmapImage.UriSource must be in a BeginInit/EndInit block 
myBitmapImage.BeginInit(); 
myBitmapImage.UriSource = new Uri(@"C:\Water Lilies.jpg"); 

// To save significant application memory, set the DecodePixelWidth or 
// DecodePixelHeight of the BitmapImage value of the image source to the desired 
// height or width of the rendered image. If you don't do this, the application will 
// cache the image as though it were rendered as its normal size rather then just 
// the size that is displayed. 
// Note: In order to preserve aspect ratio, set DecodePixelWidth 
// or DecodePixelHeight but not both. 
myBitmapImage.DecodePixelWidth = 200; 
myBitmapImage.EndInit(); 
// 
//when you are ready to render the BitmapImage, do: 
imageThumb.Source = myBitmapImage; 

नोट वांछित कम पिक्सेल आकार में छवि को कैश करने के DecodePixelWidth और DecodePixelHeight गुण। थंबनेल आकार फिट करने के लिए छवि को खींचने के लिए दोनों का उपयोग करें।

+0

बढ़िया! यह मेरे आवेदन में छवियों को बहुत तेज़ी से लोड कर देता है और मुझे प्रत्येक छवि को पूर्ण आकार में संभालने की आवश्यकता नहीं है और फिर इसे फिर से नीचे स्केल करने की आवश्यकता नहीं है। – ygoe

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