簡體   English   中英

如何在C#中獲取圖像的高度和寬度

[英]how to get the height and width of an image in C#

我試圖從網站獲取圖像的高度和寬度但它總是返回0,因為圖像尚未下載所以我使用以下代碼仍然沒有工作,因為圖像將僅在方法結束后開始下載,所以它掛了

 someMethod
   {      
    foreach(string imagepath in paths){
    IsDownloaded = false;
    image = new BitmapImage(new Uri(imagepath));
    image.ImageOpened += image_ImageOpened;
    while (!IsDownloaded) ;
    /// code that will use image.PixelHeight only if it satisfy a condition then break
   }

    private void image_ImageOpened(object sender, RoutedEventArgs e)
    {
        IsDownloaded = true;
    }

有沒有人有任何替代或任何修復這種地鐵風格的應用程序支持

您不能使用這樣的異步編程 - 刪除此行:

while (!IsDownloaded) ;

並將其后的所有內容放在image_ImageOpened方法中。 我們通常將其稱為“鏈接”,當您擁有一堆異步方法時,必須在每個方法完成后繼續處理。

從我自己的代碼獲取寬度/高度的一個例子:

        BitmapImage imageSource = new BitmapImage();
        private void getImage()
        {
            Uri uir= new Uri("PATH", UriKind.Absolute);
            imageSource.ImageOpened += new EventHandler<RoutedEventArgs>(imageopenened);
        }

        void imageopened(object sender, RoutedEventArgs e)
        {
            HEIGHT = ImageSource.PixelHeight;
            WIDTH = ImageSource.PixelWidth;
        ...
        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM