我有一个应用程序,它从数据库中加载一些blob数据,可以表示各种位图和图标的png格式或原始二进制数据.这是存储在std::vector<unsigned char>
我正在使用CImageList对象在树视图,工具栏图像等中显示各种图像,但问题是从内存中的数据创建位图会变得模糊,好像它在执行如下操作时缺少像素:
std::vector<unsigned char> bits;
HBITMAP hbitmap = CreateBitmap(16, 16, 1, 32, bits.data());
Run Code Online (Sandbox Code Playgroud)
为了解决这个问题,我现在只需将向量中的data()写入临时文件,然后使用LoadImage将其读回并从中创建HBITMAP.这是完美的,但无可否认,这是一个无耻的黑客,我希望完全没必要.
我在网上环顾四周,但没有找到任何关于如何"正确"从内存创建hbitmaps的好例子.我希望能够创建这些位图,以便添加到图像列表中,而无需任何文件i/o和有限数量的复制数据(如果可能).
寻找最好的方法来做到这一点,显然Windows特定的代码是好的.
更新:
根据jdv的回答,我开始使用CreateCompatibleBitmap,CreateDIBitmap,最后是CreateDIBSection.所有这些最终创建了可爱的黑色位图,而不是之前的模糊位图,所以我必须再次做错了我的猜测是因为这个位图创建是在一个没有屏幕直流或窗口概念的对象中完成的,使用GetDC(NULL)和CreateCompatibleDC(NULL)不好.示例代码:
BITMAPINFO bmi;
ZeroMemory(&bmi, sizeof(BITMAPINFO));
bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmi.bmiHeader.biBitCount = 32;
bmi.bmiHeader.biHeight = 16;
bmi.bmiHeader.biWidth = 16;
bmi.bmiHeader.biPlanes = 1;
HDC dc = CreateCompatibleDC(NULL);
HBITMAP hbitmap = CreateDIBSection(dc, &bmi, DIB_RGB_COLORS, (void**)blobData.GetMember<FILEDATAFIELD_DATA>().data(), NULL, 0);
Run Code Online (Sandbox Code Playgroud)
我现在当然认为必须有一个更简单的方法来解决这个问题,或许完全避免使用HBITMAP并直接与CBitmap班级合作?当它归结为将图像添加到CImageList我正在使用CBitmap::FromHandle(HBITMAP hbitmap, COLORREF mask)的时候.有谁知道CBitmap从一个初始化对象的简单方法std::vector<unsigned char>?
假设我有一个System.Drawing.Bitmap32bpp的ARGB模式.这是一个很大的位图,但它主要是完全透明的像素,在中间某处有一个相对较小的图像.
什么是快速算法来检测"真实"图像的边框,所以我可以裁掉周围的所有透明像素?
或者,我可以使用.Net中的功能吗?
可能重复:
解码后的位图字节大小?
反正所以我可以得到这个Bitmap的大小?我试过使用getByteCount()但我不能使用它?
Bitmap bitmap = BitmapFactory.decodeByteArray(decryptedData , 0, decryptedData .length); //decoding bytearrayoutputstream to bitmap
Run Code Online (Sandbox Code Playgroud)
有什么建议?
我有一个非常图片密集的Android应用程序.我目前正在使用Bitmap.createScaledBitmap()将图像缩放到所需的大小.但是,这种方法要求我已经在内存中有原始位图,这可能非常大.
如何在不首先将整个内容写入本地内存或文件系统的情况下缩放我正在下载的位图?
我有一个bitmap,所以我想上传一个webserivceas字符串,并想要检索字符串.
使用以下命令将位图转换为字符串am:
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
strBase64 = Base64.encodeToString(byteArray, Base64.URL_SAFE);
Run Code Online (Sandbox Code Playgroud)
以上String内容用作soapobject上传的属性.
但是Out of memory on a 11674900-byte allocation在打印和上传时我会得到.
如果我调试了问题,没有打印我得到
com.sun.jdi.InvocationException occurred invoking method.
Run Code Online (Sandbox Code Playgroud)
上 soaprequest.
如何解决此问题以及upload image作为字符串的webservice?
我正在尝试将图像从BitmapWindows图标转换为Windows图标.这是代码.
private void btnCnvrtSave_Click(object sender, EventArgs e)
{
Bitmap bmp = (Bitmap)picturePanel.BackgroundImage;
Bitmap newBmp = new Bitmap(bmp);
Bitmap targetBmp = newBmp.Clone(new Rectangle(0, 0, newBmp.Width, newBmp.Height), PixelFormat.Format64bppArgb);
IntPtr Hicon = targetBmp.GetHicon();
Icon myIcon = Icon.FromHandle(Hicon);
SaveFileDialog sfd = new SaveFileDialog();
sfd.Title = "Save Icon";
sfd.Filter = "Icon|*.ico";
sfd.ShowDialog();
FileStream fileStream = new FileStream(sfd.FileName,FileMode.OpenOrCreate);
myIcon.Save(fileStream);
fileStream.Flush();
fileStream.Close();
MessageBox.Show("Image is converted successfully!");
}
Run Code Online (Sandbox Code Playgroud)
代码工作正常,但问题是,当我将图片转换为图标时,转换后的图标会丢失其真实颜色和渐变(如图所示).那么,有没有什么方法可以转换图像而不会失去它的颜色?
这就是我的图标.

我想知道是否decodeResource (Resources res, int id, BitmapFactory.Options opts)考虑drawable-ldpi,mdpi,hdpi等文件夹.
我检查了源代码,它看起来不像那样,但我可能会遗漏一些东西.
(一般来说,R.drawable.在Android源代码中已解决?我无法找到它.)
有一个java.lang.ClassCastException当我试图让安装的应用程序图标.
这是我的代码.
public Bitmap getAppIcon(String path) {
PackageInfo pi = pm.getPackageArchiveInfo(path, 0);
pi.applicationInfo.sourceDir = path;
pi.applicationInfo.publicSourceDir = path;
Drawable icon = pi.applicationInfo.loadIcon(pm);
Bitmap APKicon = ((BitmapDrawable) icon).getBitmap();
return APKicon;
}
Run Code Online (Sandbox Code Playgroud)
在哪里,pm是
PackageManager pm = getPackageManager();
Run Code Online (Sandbox Code Playgroud)
并且显示的错误LogCat是
Caused by: java.lang.ClassCastException: android.graphics.drawable.NinePatchDrawable cannot be cast to android.graphics.drawable.BitmapDrawable
Run Code Online (Sandbox Code Playgroud)
希望有人可以帮助我.
在我的应用程序中,我正在尝试创建一个包含位图对象中包含的图像的电子邮件.
private void sendEmailWithBitmapAttached(){
final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.setType("plain/text");
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Email Subject");
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "Email Body");
emailIntent.setType("image/png");
ContentResolver cr = getContentResolver();
// insert the image and create a path
String imageBitmapPath = MediaStore.Images.Media.insertImage(cr, bitmapForEmail,"title", "description");
// create a uri
Uri imageUri = Uri.parse(imageBitmapPath);
emailIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
emailIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
// send the email
startActivity(Intent.createChooser(emailIntent, "Send mail..."));
}
Run Code Online (Sandbox Code Playgroud)
这在Android 2.3中运行良好.
但是使用更高版本时,会产生以下错误:
07-13 23:01:01.252: E/MediaStore(5194): Failed to insert image
07-13 23:01:01.252: E/MediaStore(5194): java.lang.SecurityException:
Permission Denial:
writing com.android.providers.media.MediaProvider
uri content://media/external/images/media from
pid=5194, uid=10151 requires
android.permission.WRITE_EXTERNAL_STORAGE, …Run Code Online (Sandbox Code Playgroud) 我在drawable目录中有一个960x1440大小的图像文件.当应用程序启动时,图像不会作为背景加载,我在logcat中看到:
位图太大而无法上传到纹理中(2880x4320,max = 4096x4096)
如果图像为960x1440,为什么会显示2880x4320?
位图通过xml加载:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/launcher_bg"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">
Run Code Online (Sandbox Code Playgroud)