我有一个wpf应用程序,我正在使用图像.要参考我使用的图像:
Uri uri = new Uri("pack://application:,,,/assemblyName;Component/myIcon.png");
BitmapImage(uri)
Run Code Online (Sandbox Code Playgroud)
如果我直接在csproj文件下添加png(其属性BuildAction = Resource),那么它可以正常工作.
但我想将它移动到csproj下的子文件夹中.关于位图\ uri的另一个问题(857732)以及与这个msdn有关的答案.所以我尝试过:
Uri uri = new Uri("pack://application:,,,/assemblyName;Component/Icons/myIcon.png");
Run Code Online (Sandbox Code Playgroud)
但那没用.
有任何想法吗?
我有一组位图.它们在某种程度上都是透明的,我事先并不知道哪些部分是透明的.我想从原始位图创建一个新的位图,它排除透明部分,但是在正方形中.我认为这张图片解释了它:

我知道如何从现有位图创建位图,但我不知道如何找出哪个部分是透明的以及如何使用它来实现我的目标.
这就是我计划这样做的方式:
public Bitmap cutImage(Bitmap image) {
Bitmap newBitmap = null;
int width = image.getWidth();
int height = image.getHeight();
newBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(newBitmap);
//This is where I need to find out correct values of r1 and r1.
Rect r1 = new Rect(?, ?, ?, ?);
Rect r2 = new Rect(?, ?, ?, ?);
canvas.drawBitmap(image, r1, r2, null);
return newBitmap;
}
Run Code Online (Sandbox Code Playgroud)
有谁知道如何实现这一目标?
编辑:
我使用以下算法来查找left,right,top和bottom值:
private int x1;
private int x2;
private int y1;
private …Run Code Online (Sandbox Code Playgroud) 我目前正在尝试保存位图图像,但背景正在变为黑色.
我可以"另存为"图像完美无缺.我也可以"保存"图像.这更难,因为我不得不覆盖现有的图像.
但是,当我"保存"我的图像时,背景变黑了.我不知道是什么导致了它.
这是我的代码:
Bitmap tempImage = new Bitmap(DrawArea);
DrawArea.Dispose();
if (extension == ".jpeg")
tempImage.Save(fileName, System.Drawing.Imaging.ImageFormat.Jpeg);
else
tempImage.Save(fileName, System.Drawing.Imaging.ImageFormat.Bmp);
DrawArea = new Bitmap(tempImage);
pictureBox1.Image = DrawArea;
tempImage.Dispose();
Run Code Online (Sandbox Code Playgroud) 我需要找到一种方法来改变Android中位图的颜色.我需要根据int价值在我的应用程序中平滑地替换/更改椭圆形图像的颜色.我需要的东西就像是myValue=5改变我的图像的颜色RED,如果myValue=322改变颜色BLUE.我发现我能做到这一点的唯一方法是使用xml文件,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval" android:padding="10dp">
<!-- you can use any color you want I used here gray color-->
<solid android:color="#cccccc"/>
<corners
android:bottomRightRadius="10dp"
android:bottomLeftRadius="10dp"
android:topLeftRadius="10dp"
android:topRightRadius="10dp"/>
</shape>
Run Code Online (Sandbox Code Playgroud)
然后在myValue更改时设置我的ImageView图像资源.但是这样我必须创建35个不同的xml文件......我认为这不是一个好主意.
那么谁能建议更好的解决方案呢?
Android的最大堆大小非常有限,每个设备都有不同的最大堆.
某些应用程序需要能够将内容(通常是图像)缓存在内存中,而不仅仅是内部/外部存储上.
当然,有很多关于处理位图和使用尽可能少的内存的好建议,但缓存也是必需的.
我已经阅读了很多可能的缓存解决方案,但是没有一种解决方案可以提供一种缓存,这种缓存可能是一种杀手级的缓存解决方案.我想要的是一个具有下一个功能的缓存机制:
无限制地使用堆,而不必担心内存不足.应用程序需要内存,没有足够的可用内存?所以释放一些(未引用的)项目(及其密钥).
线程安全/并发.
提供基于LRU的缓存,以便最近使用的项目具有更高的停留机会.
尽可能保持活力(但不会造成任何崩溃).然而,遗憾的是,在Android上,与Java相比,软/弱引用很快就被GC编辑了.
能够处理隐藏其实际大小的对象.在Android上,在API 10及以下,位图没有使用堆内存,但被认为是这样的,所以VM无法知道什么时候释放他们,因为它认为使用相同内存容量为单参考(4字节左右).这就是为什么有些解决方案可以人为地告诉每个项目的大小,以及在删除它时该怎么做.
LruCache - API 12中的一个类(您可以轻松复制其代码).
优点:#2(?),#3,#5.
缺点:#1,#4,加上你需要复制其源代码,因为它是在API 12上呈现的.
具有用于它的值的软/弱引用的hashmap,如图此处第50页,取自本讲.
优点:#1(但不删除键),#2(需要使用ConcurrentHashMap)
缺点:#3,#4,#5
MapMaker(可从 guava库获得),它类似于以前解决方案的高级版本.
优点:#1,#2
缺点:#3,#4,#5
通过番石榴库缓存解决方案.优点和缺点取决于您的选择.不确定哪种配置最符合需求,以及它是否适用于Android.遗憾的是,即使为Android编译库也无法成功.
Android查询 - 不确定它是如何工作的.看起来很容易使用,但不确定它的优点和缺点.
有人知道杀手缓存机制吗?
我不太关心功能#5,因为它非常先进,并且随着越来越多人拥有更新的Android版本,将来不会那么需要.
我有我在我的应用程序中显示的图像.它们是从网上下载的.这些图像是几乎白色背景上的对象的图片.我希望这个背景是白色的(#FFFFFF).我想,如果我看一下像素0,0(应该总是灰白色),我可以得到颜色值并用白色替换图像中的每个像素.
之前已经问过这个问题,答案似乎是这样的:
int intOldColor = bmpOldBitmap.getPixel(0,0);
Bitmap bmpNewBitmap = Bitmap.createBitmap(bmpOldBitmap.getWidth(), bmpOldBitmap.getHeight(), Bitmap.Config.RGB_565);
Canvas c = new Canvas(bmpNewBitmap);
Paint paint = new Paint();
ColorFilter filter = new LightingColorFilter(intOldColor, Color.WHITE);
paint.setColorFilter(filter);
c.drawBitmap(bmpOriginal, 0, 0, paint);
Run Code Online (Sandbox Code Playgroud)
但是,这不起作用.
运行此代码后,整个图像似乎是我想要删除的颜色.如同,现在整个图像是1纯色.
我也希望不必遍历整个图像中的每个像素.
有任何想法吗?
我有一个在Android 4.0及更高版本上开发的应用程序.(该应用不支持4.0以下的Android版本[Ice Cream Sandwich]).
该问题涉及(打印)各种图像(例如,jpeg或png)格式的DPI.
此问题与SCREEN DPI或各种Android设备的大小无关.它也与在屏幕尺寸上显示设备上的位图无关.
我使用以下代码在"位图"中加载图像文件.然后我一直在裁剪它并使用jpegCompression将其保存为JPEG格式的另一个文件.我已经能够通过以下代码执行此操作,但我无法获取加载的DPI或设置保存的图像文件的DPI.
所以我有两个问题.
1)如何在"Bitmap"中加载后,在JPEG文件中获取(打印)DPI?
2)在保存新生成的"位图"时,如何在JPEG文件中再次设置DPI?
以下是代码的一部分供参考.
FileInputStream inputStream = new FileInputStream(theSourcePhotoFilePathName);
Bitmap bitmap = null;
BitmapRegionDecoder decoder = null;
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 1;
options.inDensity = 300; // Tried this but not working.
try {
decoder = BitmapRegionDecoder.newInstance(in, false);
bitmap = decoder.decodeRegion(region, options); // the region has cropping coordinates.
} catch (IllegalArgumentException e){
Log.d("First Activity", "Failed to recycle bitmap for rect=" + region, e);
} catch (IOException e) {
Log.d("First Activity", …Run Code Online (Sandbox Code Playgroud) 我正在创建一个应用程序,在用户控制下进行绘图等操作,然后让它们保存.我试图实现这一点的方法是使用画布绘制的自定义位图,然后保存生成的位图.
一切都按预期工作,直到调用Canvas.setBitmap(Bitmap).
我收到以下错误.
03-24 13:47:50.741: E/AndroidRuntime(27888): FATAL EXCEPTION: main
03-24 13:47:50.741: E/AndroidRuntime(27888): Process: example.imageeditor, PID: 27888
03-24 13:47:50.741: E/AndroidRuntime(27888): java.lang.UnsupportedOperationException
03-24 13:47:50.741: E/AndroidRuntime(27888): at android.view.HardwareCanvas.setBitmap(HardwareCanvas.java:39)
Run Code Online (Sandbox Code Playgroud)
抛出异常的代码:
protected void onDraw(Canvas canvas) {
mResultImage=Bitmap.createBitmap(width,height,mOriginalImage.getConfig());
canvas.setBitmap(mResultImage);
canvas.save();
if(mOriginalImage!=null)
canvas.drawBitmap(mOriginalImage, width, height, paint);
else
canvas.drawText("Image loading...", width/2f-20, height/2, paint);
canvas.drawText(text, x, y-20, paint);
canvas.restore();
super.onDraw(canvas);
}
Run Code Online (Sandbox Code Playgroud)
android.view.HardwareCanvas甚至没有在android的引用上.但我能够找到一些有关它的信息.似乎它的setBitmap(Bitmap)尚未编写,这没关系.
我的问题是为什么onDraw(Canvas)返回一个HardwareCanvas类?它甚至不是Canvas的超级类.
奖金问题:有什么方法吗?
我有这个:
paint = new Paint();
paint.setAntiAlias(true);
paint.setFilterBitmap(true);
paint.setDither(true);
sm = new Matrix();
sm.setScale(scale, scale);
private Bitmap getImage(String n) {
File dir = context.getDir("theme", Context.MODE_PRIVATE);
File file = new File(dir, n + ".png");
if (file.exists()) {
return BitmapFactory.decodeFile(file.getAbsolutePath());
} else {
return BitmapFactory.decodeResource(getResources(), getResources().getIdentifier(n, "drawable", getPackageName()));
}
}
private Bitmap resizeImage(Bitmap b) {
return Bitmap.createBitmap(b, 0, 0, b.getWidth(), b.getHeight(), sm, true);
}
public void onTouchEvent(MotionEvent event) {
#scrollable bitmaps, parallax effect
updatePosition();
}
private void draw() {
current_time = System.currentTimeMillis();
if …Run Code Online (Sandbox Code Playgroud) 我需要从SD卡获取图像,创建,旋转和保存更改的图像.我尝试使用此代码
Bitmap original = BitmapFactory.decodeFile(file.getAbsolutePath());
ExifInterface originalExif = new ExifInterface(file.getAbsolutePath());
int orientation = originalExif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED);
Matrix matrix = new Matrix();
int rotate = 90;
if(orientation == ExifInterface.ORIENTATION_ROTATE_90){
rotate = 180;
}else if(orientation == ExifInterface.ORIENTATION_ROTATE_180){
rotate = 270;
}else if(orientation == ExifInterface.ORIENTATION_ROTATE_270){
rotate = 0;
}
matrix.postRotate(rotate);
Bitmap bitmap = Bitmap.createBitmap(original, 0, 0, original.getWidth(), original.getHeight(), matrix, true);
try {
FileOutputStream out = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
original.recycle();
bitmap.recycle();
} …Run Code Online (Sandbox Code Playgroud)