首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何获得在it图像视图中裁剪后从图片库中提取的图像的宽度和高度

如何获得在it图像视图中裁剪后从图片库中提取的图像的宽度和高度
EN

Stack Overflow用户
提问于 2019-05-17 04:48:37
回答 2查看 957关注 0票数 0

我试图获得图像中的图片集的确切的高度宽度,使用裁剪图书馆Image视图中显示图片/图像位置。

那么,我如何才能得到它的精确的宽度和高度呢?

下面是我的代码

  1. 从图片库中选择一个图像,然后裁剪它。
代码语言:javascript
运行
AI代码解释
复制
private void SelectImageFromGallery(object sender, EventArgs e)
                {
                    new ImageCropper
                    {
                        CropShape = ImageCropper.CropShapeType.Rectangle,
                        Success = imageFile =>
                        {
                            Device.BeginInvokeOnMainThread(() =>
                        {
                            profile_img.Source = ImageSource.FromFile(imageFile);
                            var folderPath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);

                            var pathFile = profile_img.Source
                                .ToString().Replace("/data/user/0", "/data/data");
                            File.Copy(imageFile, Path.Combine(folderPath, Path.GetFileName(pathFile)));

                        });
                    }
                }.Show(this);

            }
  1. 准备上传图片(但我需要它的宽度和高度) //获取已裁剪图像var folderPath =folderPath //的文件夹路径,将其转换为字节。var readAllBytes = File.ReadAllBytes(Path.Combine(folderPath,getPathName));// ByteArrayContent。var baContent =新ByteArrayContent(readAllBytes);

根据上面的代码,有什么方法可以得到一个图像的宽度和高度。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-05-19 23:19:52

您可以尝试使用代码这里获取照片的宽度和高度:

在你的SelectImageFromGallery

代码语言:javascript
运行
AI代码解释
复制
private async void SelectImageFromGallery(object sender, EventArgs e)
        {
            new ImageCropper
            {
                CropShape = ImageCropper.CropShapeType.Rectangle,
                Success = imageFile =>
                {
                    Device.BeginInvokeOnMainThread(() =>
                    {

                        ...

                        Size s = GetImageSize(imageFile);
                        Console.WriteLine(s);
                    });
                }
            }.Show(this);

        }

GetImageSize方法

代码语言:javascript
运行
AI代码解释
复制
 public static Size GetImageSize(string fileName)
        {

            if (Device.RuntimePlatform == Device.iOS)
            {
                UIImage image = UIImage.FromFile(fileName);
                return new Size((double)image.Size.Width, (double)image.Size.Height);
            }
            else if (Device.RuntimePlatform == Device.Android)
            {
                var options = new BitmapFactory.Options
                {
                    InJustDecodeBounds = true
                };
                fileName = fileName.Replace('-', '_').Replace(".png", "");
                var resId = Android.App.Application.Context.Resources.GetIdentifier(
                    fileName, "drawable", Android.App.Application.Context.PackageName);
                BitmapFactory.DecodeResource(
                    Android.App.Application.Context.Resources, resId, options);
                return new Size((double)options.OutWidth, (double)options.OutHeight);
            }

            return Size.Zero;
        }
票数 1
EN

Stack Overflow用户

发布于 2019-05-17 08:34:59

在Android环境中,一种方法是使用BitmapFactory类.

代码语言:javascript
运行
AI代码解释
复制
var options = new Android.Graphics.BitmapFactory.Options { InJustDecodeBounds = true };
Android.Graphics.BitmapFactory.DecodeFile(FULL_FILE_PATH_HERE, options);
Android.Util.Log.Info("MyApp", $"Width ={options.OutWidth}");
Android.Util.Log.Info("MyApp", $"Height={options.OutHeight}");
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56186756

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档