我试图在Silverlight和WCF服务之间来回传递一些图像.如果可能的话,我想传递一个System.Windows.Media.Imaging.BitmapImage,因为这意味着客户端不需要进行任何转换.
但是,在某些时候我需要将此图像存储在数据库中,这意味着图像表示必须能够转换为和从中转换byte[].我可以创建一个BitmapImage从byte[]通过读取所述阵列成MemoryStream和使用BitmapImage.SetSource().但我似乎无法找到一种方法,另一种方式转换-从BitmapImage到byte[].我错过了一些明显的东西吗?
如果它有帮助,转换代码可以在服务器上运行,即它不需要是Silverlight安全的.
我试图从文件系统上保存的文件加载一些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,或只加载一对,或大部分,并且每次运行代码时它都会改变.
有什么想法在这里发生了什么?
在过去的几天里,我一直在处理backgroundWorker的问题.我一直在浏览MSDN上的论坛和文档,但仍然没有找到答案,所以现在我想问你聪明的人.
长话短说,我有一个自定义用户控件,由ScrollViewer中的WrapPanel组成.WrapPanel包含一些在滚动到视图时通知的元素.然后,这些元素将被加载并显示图像,这就是问题所在的地方.为了不锁定gui线程,我将图像加载到BackgroundWorker中,但GUI仍然会停止.这是表示WrapPanel中包含的元素的类的代码:
class PictureThumbnail : INotifyingWrapPanelElement
{
private string path;
private Grid grid = null;
private BackgroundWorker thumbnailBackgroundCreator = new BackgroundWorker();
private delegate void GUIDelegate();
private Image thumbnailImage = null;
public PictureThumbnail(String path)
{
this.path = path;
visible = false;
thumbnailBackgroundCreator.DoWork += new DoWorkEventHandler(thumbnailBackgroundCreator_DoWork);
}
void thumbnailBackgroundCreator_DoWork(object sender, DoWorkEventArgs e)
{
BitmapImage bi = LoadThumbnail();
bi.Freeze(); //If i dont freeze bi then i wont be able to access
GUIDelegate UpdateProgressBar = delegate
{
//If this line is commented out the …Run Code Online (Sandbox Code Playgroud) 我有一个imageview
<ImageView
android:id="@+id/imgCaptured"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:adjustViewBounds="true"
android:scaleType="fitXY"
android:src="@drawable/captured_image" />
Run Code Online (Sandbox Code Playgroud)
我从相机捕获图像,将该图像转换为位图.
Bitmap thumbnail;
thumbnail = MediaStore.Images.Media.getBitmap(getActivity()
.getContentResolver(), imageUri);
Run Code Online (Sandbox Code Playgroud)
当我在上面的imageview中显示它之前得到这个位图的分辨率,比如
Log.i("ImageWidth = " + thumbnail.getWidth(), "ImageHeight = "
+ thumbnail.getHeight());
Run Code Online (Sandbox Code Playgroud)
它归还了我 ImageWidth = 2592 ImageHeight = 1936
在此之后我在上面的imageview中显示了这个位图,imgCaptured.setImageBitmap(thumbnail); 然后我将我的imageview的大小视为
Log.i("ImageView Width = " + imgCaptured.getWidth(),
"ImageView Height = " + imgCaptured.getHeight());
Run Code Online (Sandbox Code Playgroud)
这让我回头 ImageView Width = 480 ImageView Height = 720
现在我的问题是
在我的imageview中显示后,如何获得该位图的大小.我知道这可以通过使用它来完成
image.buildDrawingCache();
Bitmap bmap = image.getDrawingCache();
Run Code Online (Sandbox Code Playgroud)
但这会创建一个大小等于imageview的新位图.
我还想知道,在imageview中显示后,图像会自动调整大小.如果是,那么有没有办法在imageview中显示图像而不调整图像大小.
编辑
实际上我已经拍摄了2592x1936的图像.我在imageView中显示了这个图像,对此图像做了一些其他操作.现在我想以相同的2592x1936分辨率保存此图像.可能吗?
提前致谢.
有谁知道如何缩放位图图像而不会失去图像质量?目前我面临这个问题,所选图像的大小可能太大导致应用程序返回到另一个活动.
所以现在我尝试使用这种方法来缩放所选图像而不会降低其质量.我从这里得到了代码.
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变成一个System.Drawing.Image?
这是我的BitmapImage:
BitmapImage bmi = new BitmapImage(new Uri(someString, UriKind.Absolute));
Run Code Online (Sandbox Code Playgroud)
现在,我将如何创建一个System.Drawing.Image来自我的bmi?
我正在编写一个应用程序,要求我将大图像拆分成小图块,其中每个图块基本上是原始图像的裁剪版本.
目前我的拆分操作看起来像这样
tile.Image = new BitmapImage();
tile.Image.BeginInit();
tile.Image.UriSource = OriginalImage.UriSource;
tile.Image.SourceRect = new Int32Rect(x * tileWidth + x, y * tileHeight, tileWidth, tileHeight);
tile.Image.EndInit();
Run Code Online (Sandbox Code Playgroud)
直觉上,我认为这将基本上创建原始图像的"参考",并且只显示为图像的子矩形.然而,我的分割操作执行速度慢导致我认为这实际上是复制原始图像的源矩形,对于大图像来说这是非常慢的(当分割一个体面时有一个明显的3-4秒暂停大小图片).
我环顾四周但却找不到一种方法来绘制位图作为大图像的子矩形,而无需复制任何数据.有什么建议?
我正在尝试在我的应用中添加对平板电脑的支持,并遇到由此行代码抛出的IllegalArgumentException:
marker.setIcon(BitmapDescriptorFactory.fromResource(R.drawable.arrow_green_10by19))
Run Code Online (Sandbox Code Playgroud)
方法.fromResource可以从图像文件(png)中使用R.drawable.arrow_green_10by19,但是当用矢量文件arrow_green_10by19.xml(在Android Studio IDE中渲染得很好)替换png时,它会生成如上所述的运行时.
有人知道如何在BitmapDescriptorFactory中实现矢量资源并可以帮助我吗?
谢谢.
如何在UWP中将BitmapImage对象转换为字节数组?在.Net中很容易实现,即使在以前的WinRT版本中,看起来遍布互联网但没有成功,其中一个解决方案是使用WriteableBitmap这个答案中提到的类似,但在当前版本的UWP中,构建一个WriteableBitmap一个BitmapImage是不可能的,任何解决方法?
如何获取 System.Windows.Media.Imaging.BitmapImage 支持的图像格式列表?
我正在用 C# WPF 编写一个简单的图像处理工具。BitmapImage 类是更有用的位图类之一,因为它能够从多种格式进行解码。
特别是,它可以在我的电脑上打开 NEF(尼康的 RAW 格式)。BitmapImage 很可能可以打开来自其他制造商的各种 RAW 格式,这是我热衷于使用的功能。
由于我不知道可以作为 BitmapImage 打开的每种格式,我目前正在使用 try/catch 尝试从用户尝试打开的每个文件构建 BitmapImage。这显然不是最有效的方式。
据我所知,BitmapImage 继承自 BitmapSource,BitmapSource通过查看用户系统的可用编解码器来决定它可以打开哪些文件。因此,编解码器的可用性可能因机器而异,这意味着无法将支持的格式列表硬编码到程序中。我需要一种方法来检查用户机器上支持的这些格式是什么。
我在 System.Drawing 中找到了这个方法。这将返回支持的编解码器列表以及支持的文件扩展名列表,Systems.Windows.Media.Imaging 的等效项正是我所需要的。
bitmapimage ×10
c# ×7
wpf ×4
android ×3
bitmap ×2
image ×2
.net ×1
bytearray ×1
c#-4.0 ×1
crop ×1
imageview ×1
performance ×1
silverlight ×1
svg ×1
uri ×1
winrt-xaml ×1
wrappanel ×1
xml-drawable ×1