c# - 从路径中获取图像缩略图

标签 c# cocoa thumbnails nsimage xamarin.mac

在我的 xamarin.mac 应用程序中,我使用 SQLite 保存数据库,个人资料图片也保存在数据库中,但我在处理大图像(例如大小 4 MB)时遇到问题,我只需在上传时保存照片缩略图即可:

imgProfilePicture.Image = new NSImage (path);
Byte [] bytes = System.IO.File.ReadAllBytes (path);
_profilePic = Convert.ToBase64String (bytes);

我将base64字符串保存在数据库中..也,here是 C# 中的简单解决方案:

Image image = Image.FromFile(fileName);
    Image thumb = image.GetThumbnailImage(120, 120, ()=>false, IntPtr.Zero);
    thumb.Save(Path.ChangeExtension(fileName, "thumb"));

我需要这样的东西,需要调整图像大小而不裁剪它

最佳答案

        NSImage baseImage = new NSImage ("original.jpg", false);
        NSImage tinyImage = new NSImage (new CoreGraphics.CGSize (32, 32));
        tinyImage.LockFocus ();
        baseImage.DrawInRect (new CoreGraphics.CGRect (0, 0, 32, 32), new CoreGraphics.CGRect (0, 0, baseImage.Size.Width, baseImage.Size.Height), NSCompositingOperation.Copy, 1.0f);
        tinyImage.UnlockFocus ();

        NSBitmapImageRep rep = new NSBitmapImageRep (tinyImage.AsTiff ());
        NSData data = rep.RepresentationUsingTypeProperties (NSBitmapImageFileType.Jpeg);
        data.Save ("tiny.jpg", true);

这是 https://stackoverflow.com/a/7576710/36782转换为 C#

关于c# - 从路径中获取图像缩略图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40286099/

相关文章:

video - GData Youtube:获取缩略图

c# - 在 C# 更新面板刷新后运行 Javascript

c# - 如何让 ASP.NET Core 2.0 MVC Model Binder 绑定(bind)没有 FromBody 属性的复杂类型

macos - 无法加载 nib 文件 : MainMenu, 退出

iphone - 如何在 obj-c 上合成 C 数组的 getter 和 setter

c# - 获取任何文件的缩略图,包括 Windows XP/Vista 上的 SolidWorks

java - Android MediaStore 缩略图不存在

c# - 在线程池中的 Windows Phone 8 上创建图像缩略图

c# - C#中的字符串字符差异

objective-c - 如何将数据附加到 Cocoa 中的 NSmutableData