我需要动态加载许多(有时数百个)缩略图.出于性能原因,我需要在有限数量的请求中执行此操作,我使用单个请求/响应进行测试.我正在为响应中的图像发送二进制数据,并使用MemoryStream将它们加载到BitmapImage中.这工作正常,直到我加载超过约80个缩略图,然后我得到灾难性失败例外.为了确保我的数据没有损坏,我尝试使用相同的字节数组多次加载BitmapImage,并在80次左右加载后崩溃.
下面是从字节数组加载图像的示例,已知字节数组具有有效的图像数据(png):
private BitmapImage LoadImage(byte[] imageData)
{
BitmapImage img = new BitmapImage();
MemoryStream stream = new MemoryStream(imageData);
img.SetSource(stream); // Exception thrown here after too many images loaded.
return img;
}
Run Code Online (Sandbox Code Playgroud)
然后我使用BitmapImage作为页面上Image元素的源,但错误发生在img.SetSource(...)上面的行中.
添加GC.Collect()到我正在加载缩略图图像的循环允许我加载更多图像,所以我认为这与内存管理有关,但我不知道我能做些什么来解决问题.
我试图从文件系统上保存的文件加载一些BitmapImages.我有一个键和相对文件路径的字典.不幸的是,Uri构造函数在加载图像的方式上似乎不确定.
这是我的代码:
foreach(KeyValuePair<string, string> imageLocation in _imageLocations)
{
try
{
BitmapImage img = new BitmapImage();
img.BeginInit();
img.UriSource = new Uri(@imageLocation.Value, UriKind.Relative);
img.EndInit();
_images.Add(imageLocation.Key, img);
}
catch (Exception ex)
{
logger.Error("Error attempting to load image", ex);
}
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,有时Uris作为相对文件Uris加载,有时它们被装载为相对Pack Uris.似乎没有任何押韵或理由,哪个会以哪种方式加载.有时我会以一种方式加载所有Uris,或只加载一对,或大部分,并且每次运行代码时它都会改变.
有什么想法在这里发生了什么?
在我的应用程序中,我正在显示来自图库的图像,一旦我选择一个图像,图像应该被发送到新活动,其中所选图像将被设置为背景.但是,我能够获取图像从图库,但一旦我选择一个应用程序崩溃.谢谢提前
活动-1(图像显示在图库中,所选图像将发送到新活动)
public class Gallery extends Activity {
private static int RESULT_LOAD_IMAGE = 1;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gallery);
Button buttonLoadImage = (Button) findViewById(R.id.buttonLoadPicture);
buttonLoadImage.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
Intent i = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
Uri …Run Code Online (Sandbox Code Playgroud) 我需要在WPF控件上显示实时图像.我正在寻找使用WPF执行此操作的最快方法.
我正在使用其DLL API(AVT)从相机捕获图像.
图像由dll写入,摄像机使用IntPtr将回调上升到名为tFrame的Image结构(如下所述).像素数据存储在ImageBuffer属性中,其中InPtr为字节数组.
我知道如何从像素字节数组创建一个位图,但不是BitmapImage.因此可以创建一个Bitmap,然后从中创建一个BitmapImagem. 这里有一种从内存上的位图创建BitmapImage的方法.但我想直接从数据源(tFrame)创建BitmapImage.我怎样才能做到这一点?
我知道BitmapImage有一个CopyPixels方法,但它有一个SetPixels.
public struct tFrame
{
public IntPtr AncillaryBuffer;
public uint AncillaryBufferSize;
public uint AncillarySize;
public tBayerPattern BayerPattern;
public uint BitDepth;
public tFrameCtx Context;
public tImageFormat Format;
public uint FrameCount;
public uint Height;
public IntPtr ImageBuffer;
public uint ImageBufferSize;
public uint ImageSize;
public uint RegionX;
public uint RegionY;
public tErr Status;
public uint TimestampHi;
public uint TimestampLo;
public uint Width;
}
Run Code Online (Sandbox Code Playgroud)
这是我如何从像素字节数组创建位图.这是在WinForm版本的软件中使用的.
private void CreateBitmap(tFrame frame)
{
//This …Run Code Online (Sandbox Code Playgroud) 是否有任何方法或技术可以识别位图(png/jpeg)实际上是 360 度全景图像还是普通图像。在C#或Three.js中区分全景图像和普通图像的机制是什么?
我正在尝试从手机上的特定文件路径设置图像.文件路径是手机上的照片.文件路径看起来像这样
/storage/emulated/0/Pictures/picture.jpg
这是代码.
Bitmap image = null;
//trying to set the image from filepath
try {
image = BitmapFactory.decodeStream((InputStream) new URL(filepath).getContent());
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
if (image == null) {
//This becomes true because the image is not set
Toast.makeText(getApplicationContext(),"image == null",Toast.LENGTH_LONG).show();
}
Run Code Online (Sandbox Code Playgroud)
最后,图像未设置.
有谁知道如何缩放位图图像而不会失去图像质量?目前我面临这个问题,所选图像的大小可能太大导致应用程序返回到另一个活动.
所以现在我尝试使用这种方法来缩放所选图像而不会降低其质量.我从这里得到了代码.
protected void onActivityResult(int requestCode, int resultCode,
Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case RESULT_LOAD_IMAGE:
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK & null != data) {
Uri selectedImage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver()
.query(selectedImage, filePathColumn, null, null,
null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
Bitmap a = (BitmapFactory.decodeFile(picturePath));
photo = scaleBitmap(a, 200, 200);
imageView.setImageBitmap(photo);
}
break;
}
public static Bitmap scaleBitmap(Bitmap …Run Code Online (Sandbox Code Playgroud) 我找不到任何东西,需要一些帮助.我已经将一堆图像作为BitmapImage类型加载到内存中,这样我就可以删除它们存储的临时目录.我已经成功完成了这一部分.现在我需要将图像保存到不同的临时位置,我无法弄清楚如何执行此操作图像包含在:
Dictionary<string, BitmapImage>
Run Code Online (Sandbox Code Playgroud)
字符串是文件名.如何将此集合保存到新的临时位置?谢谢你的帮助!
我通过URI(Web或文件系统)获取图像,并希望将其编码为PNG并保存到临时文件:
var bin = new MemoryStream(raw).AsRandomAccessStream(); //raw is byte[]
var dec = await BitmapDecoder.CreateAsync(bin);
var pix = (await dec.GetPixelDataAsync()).DetachPixelData();
var res = new FileStream(Path.Combine(ApplicationData.Current.LocalFolder.Path, "tmp.png"), FileMode.Create);
var enc = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, res.AsRandomAccessStream());
enc.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Ignore, dec.PixelWidth, dec.PixelHeight, 96, 96, pix);
await enc.FlushAsync(); //hangs here
res.Dispose();
Run Code Online (Sandbox Code Playgroud)
问题是,这段代码挂起了await enc.FlushAsync().请帮忙!谢谢.
我有一个使用转换器在XAML加载的图像。而不是负载这一形象再次,我要采取的图像,并找到主色,以便能够使用网页上的其他图形。到目前为止,我有这个:
var himage = (BitmapImage)image_home.Source;
using (var stream = await himage.OpenReadAsync()) //**can't open himage this way**
{
//Create a decoder for the image
var decoder = await BitmapDecoder.CreateAsync(stream);
//Create a transform to get a 1x1 image
var myTransform = new BitmapTransform { ScaledHeight = 1, ScaledWidth = 1 };
//Get the pixel provider
var pixels = await decoder.GetPixelDataAsync(
BitmapPixelFormat.Rgba8,
BitmapAlphaMode.Ignore,
myTransform,
ExifOrientationMode.IgnoreExifOrientation,
ColorManagementMode.DoNotColorManage);
//Get the bytes of the 1x1 scaled image
var bytes = pixels.DetachPixelData();
//read the color
var myDominantColor …Run Code Online (Sandbox Code Playgroud) bitmapimage ×10
c# ×6
android ×3
wpf ×3
bitmap ×2
uwp ×2
.net ×1
image ×1
java ×1
memorystream ×1
save ×1
silverlight ×1
three.js ×1
uri ×1