这个问题是关于如何读/写,分配和管理位图的像素数据.
下面是一个如何为像素数据分配字节数组(托管内存)并使用它创建位图的示例:
Size size = new Size(800, 600);
PixelFormat pxFormat = PixelFormat.Format8bppIndexed;
//Get the stride, in this case it will have the same length of the width.
//Because the image Pixel format is 1 Byte/pixel.
//Usually stride = "ByterPerPixel"*Width
Run Code Online (Sandbox Code Playgroud)
//但并非总是如此.更多信息在bobpowell.
int stride = GetStride(size.Width, pxFormat);
byte[] data = new byte[stride * size.Height];
GCHandle handle = GCHandle.Alloc(data, GCHandleType.Pinned);
Bitmap bmp = new Bitmap(size.Width, size.Height, stride,
pxFormat, handle.AddrOfPinnedObject());
//After doing your stuff, free the Bitmap and unpin the array.
bmp.Dispose(); …Run Code Online (Sandbox Code Playgroud) 假设我有一个600x600的BitmapData,我想将其缩小到100x100.
在我的应用程序中,我想以字符串的形式将位图图像发送到服务器,我想知道有多少种方法可以将位图转换为字符串.现在我使用Base64格式进行编码和解码,它需要更多的内存.有没有其他可能以不同的方式做同样的事情,这需要更少的内存消耗.现在我正在使用此代码.
Resources r = ShowFullImage.this.getResources();
Bitmap bm = BitmapFactory.decodeResource(r, R.drawable.col);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.PNG, 100, baos); //bm is the bitmap object
byte[] b = baos.toByteArray();
String encodedImage = Base64.encodeToString(b, Base64.DEFAULT);
Run Code Online (Sandbox Code Playgroud) 我尝试从Bitmap(System.Drawing.Bitmap)获取所有字节值.因此我锁定字节并复制它们:
public static byte[] GetPixels(Bitmap bitmap){
if(bitmap-PixelFormat.Equals(PixelFormat.Format32.bppArgb)){
var argbData = new byte[bitmap.Width*bitmap.Height*4];
var bd = bitmap.LockBits(new Rectangle(0, 0, image.Width, image.Height), ImageLockMode.ReadOnly, bitmap.PixelFormat);
System.Runtime.InteropServices.Marshal.Copy(bd.Scan0, argbData, 0, bitmap.Width * bitmap.Height * 4);
bitmap.UnlockBits(bd);
}
}
Run Code Online (Sandbox Code Playgroud)
我用一个非常简单的2x2 PNG图像测试了这个图像,这个图像是我在Photoshop中创建的像素(红色,绿色,蓝色,白色).由于格式,我期望argbData中的以下值:
255 255 0 0 255 0 255 0
255 0 0 255 255 255 255 255
Run Code Online (Sandbox Code Playgroud)
但我得到了:
0 0 255 255 0 255 0 255
255 0 0 255 255 255 255 255
Run Code Online (Sandbox Code Playgroud)
但这是一种BGRA格式.有人知道为什么字节似乎被交换了吗?顺便说一句,当我直接将图像用于Image.Source时,如下所示,图像显示正确.那我的错是什么?
<Image Source="D:/tmp/test2.png"/>
Run Code Online (Sandbox Code Playgroud) 在Flash中,BitmapData对象可用于在RAM中存储位图,稍后您可以使用beginBitmapFill()方法将它们绘制到MovieClip .
如何将外部位图文件(.jpg)加载到BitmapData对象中?
即使是AS3代码也会有所帮助.
我开始使用新的flex英雄sdk将这个flex应用程序转换为在android上运行.我已经替换了所有与移动设备不兼容的组件,并使其成功运行了几次.
当我使用运行原始web应用程序的相同库时,它可以很好地工作.
然而,最大的问题是在启动移动应用程序时这些随机的无效BitMapdata错误.这是整个输出.
ArgumentError: Error #2015: Invalid BitmapData.
at flash.display::BitmapData/ctor()
at flash.display::BitmapData()
at spark.primitives::BitmapImage/http://www.adobe.com/2006/flex/mx/internal::applySource()[E:\dev\hero_private_beta\frameworks\projects\spark\src\spark\primitives\BitmapImage.as:1337]
at spark.primitives::BitmapImage/http://www.adobe.com/2006/flex/mx/internal::validateSource()[E:\dev\hero_private_beta\frameworks\projects\spark\src\spark\primitives\BitmapImage.as:1652]
at spark.primitives::BitmapImage/commitProperties()[E:\dev\hero_private_beta\frameworks\projects\spark\src\spark\primitives\BitmapImage.as:902]
at spark.primitives.supportClasses::GraphicElement/validateProperties()[E:\dev\hero_private_beta\frameworks\projects\spark\src\spark\primitives\supportClasses\GraphicElement.as:3487]
at spark.components::Group/commitProperties()[E:\dev\hero_private_beta\frameworks\projects\spark\src\spark\components\Group.as:931]
at mx.core::UIComponent/validateProperties()[E:\dev\hero_private_beta\frameworks\projects\framework\src\mx\core\UIComponent.as:8095]
at mx.managers::LayoutManager/validateProperties()[E:\dev\hero_private_beta\frameworks\projects\framework\src\mx\managers\LayoutManager.as:597]
at mx.managers::LayoutManager/doPhasedInstantiation()[E:\dev\hero_private_beta\frameworks\projects\framework\src\mx\managers\LayoutManager.as:760]
at mx.managers::LayoutManager/doPhasedInstantiationCallback()[E:\dev\hero_private_beta\frameworks\projects\framework\src\mx\managers\LayoutManager.as:1157]
Run Code Online (Sandbox Code Playgroud)
我调试了很多次,我发现它到达这个方法时
mx_internal function applySource():void
{
Run Code Online (Sandbox Code Playgroud)
在BitmapImage.as中,在此声明中
if (!bitmapData && tmpSprite)
Run Code Online (Sandbox Code Playgroud)
bitmapData == null.但是,当我运行原始web应用程序时,bitmapData不为null.应该在这行代码中确定bitmapData值
<s:BitmapImage id="smallTriangle" smooth="true" source="@Embed('/com/xploreplus/components/containers/panel/assets/images/smallTriangle.png')" />
Run Code Online (Sandbox Code Playgroud)
现在在我看来,运行移动应用程序,它无法找到图像,因为当我运行webapplication时,使用上面代码行所在的相同库,它能够找到图像.
这个问题一直困扰着我好几天,我真的开始认为这是一个错误,特别是因为它有时会运行.或者我在这里缺少一些东西.
我正在使用com.adobe.images.PNGEncoder将bitmapData编码为byteArray.有没有办法将byteArray转换回bitmapData不使用Loader?谢谢.
编辑:我不想使用Loader的原因是它是Asynchronous,我不想实现eventlisteners.
如何在AS3中将彩色位图的位图数据更改为黑白?我正在为闪存中的CMS开发一个简单的图像编辑器工具.
人们应该能够将上传的位图的颜色切换为黑白.我希望bitmapdata本身改变所以我可以用Adobe的JPGEncoder类将其写入ByteArray.
我认真地到处寻找,但找不到解决方案.我的应用程序可以轻松地将UTF字节作为XML文件保存在iOS和Android的允许缓存位置中.
但我无法弄清楚如何将Bitmap/BitmapData保存到同一缓存文件夹中的文件.创建文件很好....
_fs.open(_f, FileMode.WRITE);
//What goes in here to save the Bitmap?
_fs.close();
Run Code Online (Sandbox Code Playgroud)
任何有关这方面的帮助将不胜感激.
谢谢
更新:代码如下......
_smallLogo = Bitmap(_loader.content);
//this.addChild(_smallLogo);
var jpg:JPGEncoder = new JPGEncoder(100);
var bd:BitmapData = new BitmapData(_smallLogo.width, _smallLogo.height);
bd.draw(_smallLogo);
var ba:ByteArray = jpg.encode(bd);
//3
_fs.open(_f, FileMode.WRITE);
_fs.writeBytes( ba, 0, ba.length );
_fs.close();
Run Code Online (Sandbox Code Playgroud)
更新 - 答案
//SAVE
_smallLogo = Bitmap(_loader.content);
var jpg:JPGEncoder = new JPGEncoder(100);
var ba:ByteArray = jpg.encode(_smallLogo.bitmapData);
_fs.open(_f, FileMode.WRITE);
_fs.writeBytes( ba, 0, ba.length );
_fs.close();
Run Code Online (Sandbox Code Playgroud)
//OPEN
private function getLogo():void {
var ba:ByteArray = new ByteArray();
_fs.open(_f, …Run Code Online (Sandbox Code Playgroud) 我想将BitmapData缩放到不同的大小,如200,400,600和800.
有什么好办法呢?
bitmapdata ×10
bitmap ×4
flash ×3
c# ×2
image ×2
.net ×1
actionscript ×1
android ×1
apache-flex ×1
bytearray ×1
drawing ×1
java ×1
resize ×1