简体   繁体   English

如何在调整大小时计算图像的宽度和高度值?

[英]How to calculate width and height values of an image when resized?

I need a way to calculate the width and height values of an image when resized to 1024px 当调整大小为1024px时,我需要一种方法来计算图像的宽度和高度值

The largest value of the image, height or width will be resized to 1024px and I need to figure out the remaining width or height value. 图像,高度或宽度的最大值将调整为1024px,我需要计算剩余的宽度或高度值。

An image (3200 x 2400px) converts to (1024 x 768px) when resized. 调整大小后,图像(3200 x 2400px)将转换为(1024 x 768像素)。

This needs to be dynamic as some images will be portrait and some landscape. 这需要是动态的,因为一些图像将是肖像和一些风景。

Can anyone suggest how I'd work a solution into the following: 任何人都可以建议我如何解决以下问题:

<msxsl:script language="C#" implements-prefix="emint">
    <![CDATA[public string GetExtension(string fileName)
      { 
      string[] terms = fileName.Split('.');
      if (terms.Length <= 0)
      {
      return string.Empty;
      }
      return terms[terms.Length -1];
      }

      public string GetFileName(string fileName)
      { 
      string[] terms = fileName.Split('/');
      if (terms.Length <= 0)
      {
      return string.Empty;
      }
      return terms[terms.Length -1];
      }

      public string GetFileSize(Decimal mbs)
      { 
      Decimal result = Decimal.Round(mbs, 2);
      if (result == 0)
      {
      result = mbs * 1024;
      return Decimal.Round(result, 2).ToString() + " KB";
      }
      return result.ToString() + " MB";
      } 

      public string GetCentimeters(Decimal pix)
      {
      Decimal formula  = (decimal)0.026458333;
      Decimal result = pix * formula;
      return Decimal.Round(result,0).ToString();
      }]]>
  </msxsl:script>
          width = 1024;
          height = 768;

          ratio_orig = width_orig/height_orig;

          if (width/height > ratio_orig) {
             width = height*ratio_orig;
          } else {
             height = width/ratio_orig;
          }

The values of width and height at the end correspond to the width and height of the image. 末端的widthheight值对应于图像的宽度和高度。 This maintains the aspect ratio. 这保持了纵横比。

Here's an algorithm in pseudocode. 这是伪代码的算法。 It will choose the largest possible size (less than 1024x1024px) that has the same width/height ratio as the original image. 它将选择与原始图像具有相同宽度/高度比的最大可能尺寸(小于1024x1024px)。

target_width = 1024
target_height = 1024

target_ratio = target_width / target_height
orig_ratio = orig_width / orig_height

if orig_ratio < target_ratio
    # Limited by height
    target_width = round(target_height * orig_ratio)
else
    # Limited by width
    target_height = round(target_width / orig_ratio)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 调整视图大小后如何检索图像的高度和宽度 - How to Retrieve the height and width of an Image after it is resized for a View 当仅在HTML中明确指定调整大小的宽度时,使用javascript得出图像的高度 - Using javascript to derive height of image when only resized width is explicitly given in html 如何使用调整后的图像比例调整顶部/左侧/宽度/高度 - How to adjust the top/left/width/height based on the ratio of the resized image,using jquery 将图片插入DOM,尺寸调整为其宽度/高度的50% - Insert image to DOM resized to 50% of its width/height 调整window.width的大小但不调整高度时执行功能 - Perform function when window.width is resized but not the height 计算图像地图上多边形区域的宽度和高度 - calculate width & height of poly area on image map 调整大小时如何同时获得屏幕的宽度和高度? - How to get screen width and height simultaniously while being resized? 如何根据输入框中的值更改图像的宽度和高度? - How to change the width and height of image based on values from input box? 调整窗口高度时调整图像的大小 - Adjust size of image when the window's height is resized 在将图像加载到页面上之前,如何使用jQuery计算图像的宽度和高度? - How do I calculate the width and height of an image with jQuery, before I load the image on a page?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM