Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here's a method for cropping a wider image, so that you can understand the concept.</p> <p>First calculate the extra width of the image. i.e how much additional space it is taking more than a 4:3 ratio of the same.</p> <p>Consider I want to crop a 1366 x 768 picture to 1024 x 768.</p> <p>Here we can calculate the extra_width with the height (768px):</p> <pre><code>4 / 3 * (768) = 1024 </code></pre> <p>So, that gives you the target width for a 768 height.</p> <p>Now the extra width is:</p> <pre><code>1366 - 1024 </code></pre> <p>Now you can crop the image by putting the starting crop point to 1/2 of the extra_width, and select the full_height.</p> <pre><code> Image cropImg(Image img) { // 4:3 Aspect Ratio. You can also add it as parameters double aspectRatio_X = 4; double aspectRatio_Y = 3; double imgWidth = Convert.ToDouble(img.Width); double imgHeight = Convert.ToDouble(img.Height); if (imgWidth / imgHeight &gt; (aspectRatio_X / aspectRatio_Y)) { double extraWidth = imgWidth - (imgHeight * (aspectRatio_X / aspectRatio_Y)); double cropStartFrom = extraWidth / 2; Bitmap bmp = new Bitmap((int)(img.Width - extraWidth), img.Height); Graphics grp = Graphics.FromImage(bmp); grp.DrawImage(img, new Rectangle(0, 0, (int)(img.Width - extraWidth), img.Height), new Rectangle((int)cropStartFrom, 0, (int)(imgWidth - extraWidth), img.Height), GraphicsUnit.Pixel); return (Image)bmp; } else return null; } private void button1_Click(object sender, EventArgs e) { pictureBox2.Image = cropImg(pictureBox1.Image); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload