【问题标题】:Crop Image Issue when not showing Actual Image using C#使用 C# 不显示实际图像时出现裁剪图像问题
【发布时间】:2015-04-15 07:40:35
【问题描述】:

我正在使用JCrop 裁剪Image。如果我向用户显示实际图像,它工作正常。但是,如果我显示 Resize Image 而不是 Actual Image,那么我得到的是 Resize ImageCo-ordinates

那么,我如何根据它裁剪Image?在这里,我传递了 Image 的保存路径 Image

简而言之,如果 Saved Image size if for i.e. 715 * 350 然后我会在基于 CSS 的 Small Size 弹出窗口中显示它。所以,我会得到那个小号ImageCo-ordinates。我在 Main Image 上应用那些 Co-ordinates

我的代码:

using (System.Drawing.Image OriginalImage = System.Drawing.Image.FromFile(Img))
            {
                using (System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(Width, Height))
                {
                    bmp.SetResolution(OriginalImage.HorizontalResolution, OriginalImage.VerticalResolution);

                    using (System.Drawing.Graphics Graphic = System.Drawing.Graphics.FromImage(bmp))
                    {
                        Graphic.SmoothingMode = SmoothingMode.AntiAlias;
                        Graphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
                        Graphic.PixelOffsetMode = PixelOffsetMode.HighQuality;
                        Graphic.DrawImage(OriginalImage, new System.Drawing.Rectangle(0, 0, Width, Height), X, Y, Width, Height, System.Drawing.GraphicsUnit.Pixel);

                        MemoryStream ms = new MemoryStream();
                        bmp.Save(ms, OriginalImage.RawFormat);

                        ms.Close();
                        ms.Flush();
                        ms.Dispose();

                        return ms.GetBuffer();
                    }
                }
            }

【问题讨论】:

    标签: c# image jcrop


    【解决方案1】:

    Jcrop 有 tureSize 属性。

    $.Jcrop('#image',{ trueSize: [715, 350] });
    

    你应该得到大图的正确坐标。

    http://deepliquid.com/content/Jcrop_Sizing_Issues.html

    【讨论】:

      【解决方案2】:

      您显示的代码用于调整大小,而不是用于裁剪(在Graphic.DrawImage() 调用中,您不关心裁剪坐标,只需应用目标宽度/高度)

      要裁剪图像,您可以使用Bitmap.Clone() 方法。只需将您从JCrop 提取的裁剪坐标传递给它。 (以下示例中为cropzone

      public static async Task CropImage()
      {
          var client = new WebClient();
          var sourceimg = new Uri(@"http://logonoid.com/images/stack-overflow-logo.png");
          var destination = new FileInfo(Path.Combine(Directory.GetCurrentDirectory(), "logoCropped.png"));
          if (destination.Exists)
              destination.Delete();
          using (Stream sourceStream = await client.OpenReadTaskAsync(sourceimg))
          {
              using (Bitmap source = new Bitmap(sourceStream))
              {
                  Rectangle cropzone = new Rectangle(0, 0, 256, 256);
                  using (Bitmap croppedBitmap = source.Clone(cropzone, source.PixelFormat))
                  {
                      croppedBitmap.Save(destination.FullName, ImageFormat.Png);
                  }
              }
          }
      }
      

      关于您的代码的一些建议:

      • 仅在裁剪时,不涉及调整大小操作。所以SmoothingModeInterpolationModePixelOffsetMode参数在这里是没用的。
      • 关于MemoryStream,您最好在using 语句中使用它。它避免了手动调用Close()Dispose(),并保证无论发生什么都会调用它们。关于Flush() 方法,它在MemoryStream 类上just does nothing

      【讨论】:

        猜你喜欢
        • 2017-05-22
        • 2013-03-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-09-30
        • 2019-10-23
        • 2020-11-09
        • 1970-01-01
        相关资源
        最近更新 更多