简体   繁体   English

如何将图像转换为 Bitmap 或字节数组

[英]How to convert Image to Bitmap or Byte Array

I want to translate an Image into a MemoryStream or an array of bytes, but this does not work, I used to translate in Windows Forms in the same way as I did in Xamarin.Forms?我想把一个Image翻译成一个MemoryStream或者一个字节数组,但是这不起作用,我以前在Windows Forms中翻译的方式和我在Xamarin.Forms中的翻译方式一样吗?

MemoryStream ms = new MemoryStream();
img.Save(ms, img.Image.RawFormat);
byte[] byteimg = ms.ToArray();

Unlike Windows.Forms or WPF where the Image class has direct access to pixels and can provide the entirety of image manipulation APIs, Image class in Xamarin Forms is just an abstraction for multiple platform views across OSes (Android, iOS, macOS etc.) It provides limited capabilities when compared to any of the platform controls and basically is tasked with creating a platform view, loading picture from one of predefined sources (embedded resource, file, byte stream etc.), placing it somewhere in the visual tree. Unlike Windows.Forms or WPF where the Image class has direct access to pixels and can provide the entirety of image manipulation APIs, Image class in Xamarin Forms is just an abstraction for multiple platform views across OSes (Android, iOS, macOS etc.) It provides limited与任何平台控件相比的功能,主要任务是创建平台视图,从预定义源之一(嵌入式资源、文件、字节 stream 等)加载图片,将其放置在可视化树中的某个位置。 That's it, XF Image class is not tasked with low level pixel manipulation and doesn't provide access to the actual bit data of a native image.就是这样,XF Image class 不负责低级像素操作,也不提供对原始图像的实际位数据的访问。 You won't find methods that give you access to pixel and as such don't allow you to create a Stream from a loaded into memory byte data.你不会找到让你访问像素的方法,因此不允许你从加载到 memory 字节的数据中创建 Stream。

In order to get access to individual bytes you can:为了访问单个字节,您可以:

  • Look at the direction of 3rd party controls, that provide access to pixels.查看提供对像素的访问的第 3 方控件的方向。 EG you can use Skia and use it's Canvas EG 你可以使用 Skia 并使用它的 Canvas
  • Trace the source XF Image (file, resource) and load it into memory as .NET stream追踪源XF Image(文件、资源)加载到memory为.NET stream
  • Write own native code for any of the target platforms (eg create a platform service and use dependency service OR create own visual element with platform renderers) which will have access to image data of native image view为任何目标平台编写自己的本机代码(例如创建平台服务并使用依赖服务或使用平台渲染器创建自己的视觉元素),这些代码将可以访问本机图像视图的图像数据

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM