网页中的图像 asp.net c#
目标:根据分辨率调整图像大小
问题:如果我们不调整大小,对齐和图像结构将消失
需要以下代码:< /strong> 我不想将小图像调整为 100% 宽度。
System.Drawing.Image img = System.Drawing.Image.FromFile(imgTest.Src);
if (img.HorizontalResolution > 1000)
{
imgTest.Attributes.Add("width", "100%");
}
我的方式正确吗?或者还有其他替代方法可以做同样的事情吗?让我知道,我可以使用任何 CSS 替换 C# 代码吗?
Aim: To resize the images based on the resolution
Problem : If we do not resize, the alignment and image structuring will be gone
Need of the following code : I don't want to resize a small images to 100% width.
System.Drawing.Image img = System.Drawing.Image.FromFile(imgTest.Src);
if (img.HorizontalResolution > 1000)
{
imgTest.Attributes.Add("width", "100%");
}
Am I in the right way ? Or is there any other alternatives to do the same ? Let me know, can I replace the C# code using any CSS ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你能说得更具体一些吗?
基本上你是以新尺寸存储图像的吗?
或者您只是希望在需要一定宽度/高度的 DIV 或 td 内显示图像?
如果我理解正确的话,您只是想调整较大图像的大小,因此低于阈值的任何内容都不应该调整大小,对吧?
如果显示很简单,您可以放弃所有这些,只使用 CSS。
这将使任何超出这些限制的图像变得更小,但具有固定的宽高比。
此链接解释了这些 CSS 属性的基础知识 http://www.w3schools.com/cssref/ pr_dim_max-height.asp
让我知道这是否有帮助!干杯!
Can you be more specific?
Basically are you storing the image somewhere in the new size?
Or do you simply wish to show the image inside a DIV or td which needs to be of a certain width / height?
And if I understand you right, you are just looking to resize larger images, so anything below a threshold should not get resized, right?
If it is simple for display, you can ditch all of this and just use CSS
This will make any image that exceeds either of these constraints smaller but with fixed aspect ratio.
This link explains the basics of these CSS properties http://www.w3schools.com/cssref/pr_dim_max-height.asp
Let me know if this was helpful! Cheers!
您可以使用 CSS 媒体查询来执行此操作。注意:网页设计师当前的趋势是使用所谓的“响应式网页设计”技术来为移动设备实现这一目标;我的建议:不要追随这种时尚。媒体查询仅在所有标记和图像下载完毕后才会启动。因此,如果您想为移动设备应用较小的图像,仍然会下载所有桌面版本,从而导致严重的性能问题。
如果您尝试对移动设备执行此操作,请为移动设备创建单独的网站并对其应用 CSS 媒体查询。使用 Mobi 检测浏览器功能。
如果它只是一个常规的桌面浏览器站点,那么 CSS 媒体查询就是最佳选择。
http://www.w3.org/TR/css3-mediaqueries/
优势使用此方法而不是设置最大宽度和高度可以保持图像的质量。您可以通过为媒体查询提供较小的版本来实现此目的。
You could use CSS media queries to do this. CAVEAT: there is a current trend among Web Designers to use so-called "Responsive Web Design" techniques to achieve this for mobile devices; my advice: don't follow this fad. Media queries only kick in after all markup and images have downloaded. So if you wanted to apply smaller images for mobile devices, all the desktop versions would still be downloaded causing a significant performance problem.
If you are trying to do this for mobile devices, create separate site for mobile and apply the CSS media queries to that. Use Mobi to detect browser features.
If it's just a regular desktop browser site, CSS Media Queries are the way to go.
http://www.w3.org/TR/css3-mediaqueries/
The advantage of using this method over setting max width and height is that you preserve the quality of the image. You do this by having smaller versions available for the media query.