2014-02-14 96 views
1

我上Windows 8 Phone app的工作,我有一些圖片在我的應用程序顯示,這是我的圖像是非常大的以優良的品質,現在在我的應用程序,我需要調整圖像大小而不會影響寬高比。如何調整圖像,而不會干擾寬高比

...我已經尋找它,無法找到合適的soultion。

如何實現這一目標?

這是我在cs文件的代碼。

string imageName= "path to folder" + name + ".png"; 
BitmapImage bmp = new BitmapImage(new Uri(imageName, UriKind.Relative)); 
Image.Source = bmp; 

編輯

更多信息:目前,我在我的列表框顯示圖像,所以圖像看起來非常大,所以我想將它降低到較低的大小而不影響的縱橫比圖片。

+0

你在做什麼與調整大小的圖像?意圖是爲了在屏幕上顯示更小的尺寸?如果是這樣,則不需要生成另一個圖像。 –

+0

@WaltRitscher是的,我有大尺寸的圖像,但我想告訴他們在小尺寸看起來不錯,我需要得到高度和圖像的widht rendred在屏幕上之前,如果你有答案,請把你的答案 – Goofy

回答

1

這應該這樣做:

static void Main(string[] args) 
    { 
     int _newWidth = 60; //the new width is set, the height will be calculated 

     var originalImage = Bitmap.FromFile(@"C:\temp\source.png"); 

     float factor = originalImage.Width/(float)_newWidth; 
     int newHeight = (int)(originalImage.Height/factor); 

     Bitmap resizedImage = ResizeBitmap(originalImage, _newWidth, newHeight); 

     resizedImage.Save(@"c:\temp\target.png"); 
    } 

    private static Bitmap ResizeBitmap(Image b, int nWidth, int nHeight) 
    { 
     Bitmap result = new Bitmap(nWidth, nHeight); 
     using (Graphics g = Graphics.FromImage(result)) 
      g.DrawImage(b, 0, 0, nWidth, nHeight); 
     return result; 
    } 
+0

你可以請解釋它將如何不影響圖像質量在這裏,我是新的Windows 8應用程序開發 – Goofy

+0

看起來像你在C盤保存圖像︰here resizedImage.Save(@「c:\ temp \ target。 PNG「); ,但我在這裏提到我的工作有關的Windows Phone 8的Windows 8手機應用程序更新發育 – Goofy

+0

不知道,但我想加載它會Bitmap.FromStream和保存resizedImage.Save(流) – Manuel

3

如果你想降低圖像加載到內存中,然後設置DecodePixelWidth無需設置 DecodePixelHeight(或圓形其他方式)

BitmapImage bitmapImage = new BitmapImage(); 
bitmapImage.DecodePixelWidth = 80; 
bitmapImage.UriSource = new Uri(imageName, UriKind.Relative); 

編輯

或者如果你想保持高分辨率圖像的內存設置大小爲Image控制。

<Image ... Width="80"/> 

Stretch屬性默認設置爲Uniform這意味着:

的內容被調整大小以適合於目標尺寸而它保留其原始長寬比。

+0

你可以請解釋它將如何不影響圖像的質量在這裏,我是新的Windows 8應用開發 – Goofy

+0

所以你想減少它僅用於查看和保持高分辨率的圖像在內存中或保持在內存中減少? – dkozl

+0

請參閱我的編輯 – Goofy

1

如果你想按比例減少對顯示圖像的大小,儘量爲證明和this blog post很好的解釋與Image控件的屬性Stretch打。

<Image x:Name="Image" Stretch="UniformToFill"></Image> 
0

圖像元素的大小受其包含面板的影響,但這應該在任何面板中都有效。

<Grid> 
    <Image Source='flower.png' Width='120' Stretch='Uniform'/> 
    </Grid> 
相關問題