Auf meinem Entwicklungssystem (Win 10 Pro, VS 2015) teste ich eine Webformular-App in VS 2015; Es verwendet eine Masterseite. Ein Bild-Steuerelement in einer asp-Inhaltsseite zeigt nichts an, wenn ich versuche, ein Foto aus einer Datei anzuzeigen, die gerade nach einer Größenänderung geschrieben wurde, während es das Originalfoto gut anzeigt. Hier ist das Code-Behind-Fragment mit dem Problem in den Kommentaren aufgeführt:Image.ImageUrl = ... wird nichts angezeigt
// 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;
Gleiches Verhalten, ob im Debug-Modus ausgeführt wird oder nicht.
Vielleicht gibt es einen besseren Ansatz als das resized Foto in die temporäre Datei tempImg.jpg schreiben und dann daraus lesen?
Update nach Jignesh der und Antworten Sami:
I MapPath in Default.aspx.cs bin mit (siehe neuen Kommentaren unten).
Jetzt mit System.Uri, um den Pfad zu Url zu konvertieren, der mit "file: ///" beginnt. Führt dazu, dass nichts angezeigt wird. Das Ersetzen von "file:" mit "http:" führt dazu, dass ein beschädigtes Bildsymbol angezeigt wird. Aktualisiert Code ist:
// 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.
Aber was bedeutet Arbeit wird mit einer Tilde „~“ auch wenn es nicht wie eine URL sieht. Ist es in Ordnung, die Tilde zu verwenden? Wird es mich in Schwierigkeiten bringen, etwa bei der Bereitstellung auf einem Hosting-Server?
sein sollte, weil ein Browser keinen Zugriff auf Ihre Festplatte hat. Sie müssen ihm eine URL geben, keinen Pfad. Nehmen Sie auch keine manuellen Änderungen von URLs zu Pfaden vor, dafür steht 'MapPath()'. –