2016-12-08 19 views
0

私の開発システム(Win 10 Pro、VS 2015)では、VS 2015でWebフォームアプリケーションをテストしています。マスターページを使用します。サイズ変更後に書き込まれたファイルから写真を表示しようとすると、ASPコンテンツページの画像コントロールに何も表示されませんが、元の写真が表示されます。コメントに詳細が記述されたコードビハインドフラグメントがあります。Image.ImageUrl = ...何も表示されない

// During testing, sRelLoc contains "~/i/SlideShow/1th.jpg" (original photo) 
    string sRelLocMod = sRelLoc.Replace("~/", "").Replace("/", "\\"); 
    // During testing, Global.sgHomeDir contains "K:\\DataAndDocs\\12_Projects\\TinyNP\\TinyNP\\" 
    string sImgUrl = Global.sgHomeDir + sRelLocMod; 
    // sImgUrl now contains 
    System.Drawing.Image Photo = null; 
    // Read original photo from file 
    using (FileStream fs = new FileStream(sImgUrl, FileMode.Open, FileAccess.Read)) 
    { 
     Photo= System.Drawing.Image.FromStream(fs); 
    } 
    // ResizeImage from https://www.codeproject.com/articles/191424/resizing-an-image-on-the-fly-using-net 
    System.Drawing.Image PhotoResized = ResizeImage(Photo, new Size(400, 400), true); 
    sImgUrl = Global.sgHomeDir + "i\\tempImg.jpg"; 
    using (FileStream fs = new FileStream(sImgUrl, FileMode.Open, FileAccess.Write)) 
    { 
     PhotoResized.Save(fs,System.Drawing.Imaging.ImageFormat.Jpeg); 
    } 
    // NOTE THAT: The image has been saved correctly in its resized form inside the expected directory 
    sImgUrl = sImgUrl.Replace("\\", "/"); 
    //sImgUrl now contains "K:/DataAndDocs/12_Projects/TinyNP/TinyNP/i/tempImg.jpg" 
    // Image1 is an Image control on an asp.net web page. 
    // PROBLEM: The following statement displays nothing where Image1 is 
    //   placed (not even a red-x or anything) , ... 
    Image1.ImageUrl = sImgUrl; 
    // ... but displays the unresized original just fine when the following statement is used instead: 
    //   Image1.ImageUrl = sRelLoc; 

デバッグモードで実行しているかどうかにかかわらず同じ動作です。

おそらく、サイズ変更された写真を一時ファイルtempImg.jpgに書き込んだり読み込んだりするのがよいでしょうか? jigneshさんとサミの応答後

更新:

私はDefault.aspx.csでMapPathのを(下の新しいコメントを参照)を使用しています。

System.Uriを使用して、パスを "file:///"で始まるURLに変換します。何も表示されません。 "file:"を "http:"に置き換えると、破損したイメージシンボルが表示されます。更新されたコードは次のとおりです。

// During testing, sRelLoc contains "~/i/SlideShow/1.jpg" (original photo) 
    string sRelLocMod = sRelLoc.Replace("~/", "").Replace("/", "\\"); 
    // During testing, Global.sgHomeDir contains "K:\\DataAndDocs\\12_Projects\\TinyNP\\TinyNP" 
    // which was obtained in Default.aspx.cs Page Load Event by 
    // Global.sgHomeDir = HttpContext.Current.Server.MapPath(null); 
    string sImgPath = Global.sgHomeDir + "\\" + sRelLocMod; 
    // sImgPath now contains "K:\\DataAndDocs\\12_Projects\\TinyNP\\TinyNP\\i\\SlideShow\\1.jpg" 
    System.Drawing.Image Photo = null; 
    // Read original photo from file 
    using (FileStream fs = new FileStream(sImgPath, FileMode.Open, FileAccess.Read)) 
    { Photo= System.Drawing.Image.FromStream(fs); } 
    // ResizeImage from https://www.codeproject.com/articles/191424/resizing-an-image-on-the-fly-using-net 
    System.Drawing.Image PhotoResized = ResizeImage(Photo, new Size(400, 400), true); 
    sImgPath = Global.sgHomeDir + "\\i\\tempImg.jpg"; 
    // sImgPath now contains "K:\\DataAndDocs\\12_Projects\\TinyNP\\TinyNP\\i\\tempImg.jpg" 
    using (FileStream fs = new FileStream(sImgPath, FileMode.OpenOrCreate, FileAccess.Write)) 
    { PhotoResized.Save(fs, System.Drawing.Imaging.ImageFormat.Jpeg); } 
    // I have verified that the image has been saved correctly in its resized form inside the expected directory 
    System.Uri ImgUrl = new System.Uri(sImgPath, UriKind.Absolute); 
    // Image1 is an Image control on an asp.net web page. 
    string sImgUrl = ImgUrl.ToString(); 
    //sImgUrl now contains "file:///K:/DataAndDocs/12_Projects/TinyNP/TinyNP/i/tempImg.jpg" 
    Image1.ImageUrl = sImgUrl; 
    // The above statement DOES NOT work (displays nothing at all) 
    // However: 
    //Image1.ImageUrl = "~/i/tempImg.jpg"; // ** DOES ** work ok 
    // If I replace "file:" with "http:", a broken image symbol is displayed. 

しかし、どのような作業を行うことは、それがURLのように見えていなくても、「〜」チルダを使用しています。チルダを使用しても問題ありませんか?ホスティングサーバーに配備するときのような問題に遭遇しますか?

+0

ようにする必要があります。パスではなくURLを指定する必要があります。また、URLからパスへの手動変更は行わないでください。これが 'MapPath()'の目的です。 –

答えて

0

ImageUrlプロパティを使用して、Imageコントロールに表示するイメージのURLを指定します。相対URLまたは絶対URLを使用できます。相対URLは、サーバー上の完全なパスを指定せずに、イメージの場所とWebページの場所を関連付けます。パスはWebページの場所に関連しています。これにより、コードを更新せずにサイト全体をサーバー上の別のディレクトリに移動することが容易になります。絶対URLは完全なパスを提供するため、サイトを別のディレクトリに移動するには、コードを更新する必要があります。

絶対URLとは、URLへのIISパス全体を指します(ディスクディレクトリパスではありません)。 (すなわち、http://yourVirtualDirectory/ExternalImages/logo.jpg)。そこでここでは上記のコードsImgUrlで

=「K:/DataAndDocs/12_Projects/TinyNP/TinyNP/i/tempImg.jpgは、」ブラウザがディスクにアクセスできないので、それはhttp://yourVirtualDirectory/tempImg.jpg

関連する問題