我尝试将 CSS 高度设置为自动,但是图像没有显示,所以我不得不设置一个固定高度。我试过调整大小模式,但也没有用。我尝试添加“img-responsive”但它也没有用。
CSS
.imgFile {
width: 100%;
max-width: 100%;
height: 100px;
max-height:100%;
margin-left: 7px;
margin-right: 0px;
margin-top: 5px;
margin-bottom: 1px;
background-repeat: no-repeat;
background-color: white;
background-size: 100%, 100%;
}
Asp.net
System.Web.UI.WebControls.Image image = new
System.Web.UI.WebControls.Image();
image.ID = file.Name + file.Oid;
image.Attributes.Add("class", "imgFile");
image.Attributes.Add("class", "img-responsive");
image.Style.Add("background-image", "'LogoHeaderHandler.axd?f="
+ file.Oid + "&" + DateTime.Now.ToFileTime() + "'");
最佳答案
看起来你有 2 个问题。
1) Attributes.Add 正在根据您当前的代码清除您的 imgFile 类。如果两者都需要,则必须将它们添加到一行中,如下所示:
image.Attributes.Add("class", "imgFile img-responsive");
//image.Attributes.Add("class", "img-responsive");
2) 您的 CSS 具有以下内容:background-size: 100%, 100%;这需要删除以保留图像的“原始”大小,否则会拉伸(stretch)图像。
你的其余代码似乎给了我你正在寻找的结果(*假设你的处理程序按预期工作......我没有在我的测试中模拟这部分)。
关于c# - 如何在图像控件中显示原始大小的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50706036/