我想要做的是如此简单,但我很难让它工作.我在同一行看到了一些帖子,但我仍然有疑问.
我有一个名为mnuA的MenuItem对象.我想要的只是在C#中以编程方式设置icon属性.我尝试了以下内容
a)mnuA.Icon = new BitmapImage{UriSource = new Uri(@"c:\icons\A.png")};
结果:我没有显示实际的图标,而是获得了类名(System.Windows.Media.Imaging.BitmapImage)
b)mnuA.Icon = new BitmapImage(new Uri(@"c:\icons\A.png"));
结果:我没有显示实际图标,而是获得图像的路径(file:///c:/icons/A.png)
我究竟做错了什么?对于像这样简单的事情,我真的需要一个转换器类吗?
我有几个图像文件,我想在项目之间共享(常见的图标)我在一个程序集中将它们放在我创建的每个解决方案中...我将文件放在一个名为Icon的文件夹中,我将构建作为内容副本始终.我已经验证使用这些图标创建了一个文件夹...但是我的其他程序集无法找到它们...
<r:RibbonGroup Header="Users">
<r:RibbonButton >
<r:RibbonButton.LargeImageSource>
<BitmapImage UriSource="..\Icons\UserIcon.png" />
</r:RibbonButton.LargeImageSource>
</r:RibbonButton>
</r:RibbonGroup>
Run Code Online (Sandbox Code Playgroud)
我尝试过几种方式格式化uri ...但它永远不会成功.如果图标在实际装配中虽然它们有效...
如何在源代码为Build Action = Resource的图像时以编程方式创建位图图像?
当我尝试以下操作时,我得到一个无效的URI异常:(
BitmapImage image = new BitmapImage(new Uri("/MyAssembly;component/Images/MyImage.png"));
Run Code Online (Sandbox Code Playgroud)
我正在使用silverlight 4,这个代码位于我的解决方案的许多项目之一(它是一个silverlight类库而不是silverlight应用程序).
我正在尝试学习如何与图像的某些WinRT(Metro)对象进行交互.基本上,我正在努力创造一个"GetPixel"和"SetPixel"方法很容易使用(类同什么是在不能在Metro应用程序中使用System.Drawing.Bitmap).
为了测试,我创建了一个"Bitmap"类.它基于我从Metro文件选择器控件获得的IRandomAccessStream加载.我能够将像素数据加载到Byte数组中,然后从中创建一个BitmapImage,当我将其投入到Image控件中时,它会在XAML表单上正确呈现.
这是我的问题,我可以查看字节数组并查看单个ARGB值,但是它的像素数远远少于它应该的数量.例如,我正在加载的图像是254x197像素(254*197*4 = 200,152).当我使用下面的代码加载我的字节数组时,它只有16,382字节(也没有平均除以4).我想,这是压缩的吗?我不知道.
我的问题是,我在做什么错.我想返回一个代表,我应该有这样我就可以创建GetPixel的50038个像素(X,Y)和SetPixel(X,Y,彩色)方法200152个字节.
Public Class Bitmap
Public Sub New()
End Sub
Public Async Function Load(s As Windows.Storage.Streams.IRandomAccessStream) As Task
Dim dr As New DataReader(s.GetInputStreamAt(0))
Await dr.LoadAsync(s.Size)
Me.Pixels = New Byte(s.Size - 1) {}
dr.ReadBytes(Me.Pixels)
' We're going to get the height and the width of the image this way. ;)
Dim bi As New BitmapImage
Dim stream As New MemoryStream(Me.Pixels)
bi.SetSource(New RandomStream(stream))
Me.Width = bi.PixelWidth
Me.Height = bi.PixelHeight
End Function
Public Function ToBitmapImage() As BitmapImage
Dim bi …Run Code Online (Sandbox Code Playgroud) bytearray bitmapimage microsoft-metro windows-8 windows-runtime
我想从XAML(文本)文件创建具有所需分辨率的BitmapImage.我怎样才能做到这一点?
谢谢.
我在服务器上有多个图像.我想要做的是我想从服务器检索这些图像并将其设置在imageView上.
我一直在从服务器获取blob类型的图像,将其解码为字节数组,然后将其转换为位图图像.
我很困惑如何从服务器获取位图图像.
我经历过很多关于SO的问题
任何人都可以通过链接或代码提供帮助.
谢谢你宝贵的时间.
我正在尝试的东西,这里是代码:
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Bitmap bitmap = DownloadImage("http://www.allindiaflorist.com/imgs/arrangemen4.jpg");
ImageView img = (ImageView) findViewById(R.id.img);
img.setImageBitmap(bitmap);
}
private InputStream OpenHttpConnection(String urlString)
throws IOException
{
InputStream in = null;
int response = -1;
URL url = new URL(urlString);
URLConnection conn = url.openConnection();
if (!(conn instanceof HttpURLConnection))
throw new IOException("Not an HTTP connection");
try{
HttpURLConnection httpConn = (HttpURLConnection) conn;
httpConn.setAllowUserInteraction(false);
httpConn.setInstanceFollowRedirects(true);
httpConn.setRequestMethod("GET");
httpConn.connect();
response = …Run Code Online (Sandbox Code Playgroud) 我有一个包含静态ImageSource对象的类,这些对象以后经常被其他类访问:
public class ImagePrepare
{
public static readonly ImageSource m_imageGreen;
public static readonly ImageSource m_imageYellow;
public static readonly ImageSource m_imageRed;
public static readonly ImageSource m_imagePurple;
public static int iTest;
//static Constructor
static ImagePrepare()
{
iTest = 2;
Uri uriImage = new Uri("pack://application:,,,/" + Assembly.GetExecutingAssembly().GetName().Name + ";component/" + "Ressourcen/Button_Green.png", UriKind.Absolute);
ImageSource m_imageGreen = new BitmapImage(uriImage);
uriImage = new Uri("pack://application:,,,/" + Assembly.GetExecutingAssembly().GetName().Name + ";component/" + "Ressourcen/Button_Yellow.png", UriKind.Absolute);
ImageSource m_imageYellow = new BitmapImage(uriImage);
uriImage = new Uri("pack://application:,,,/" + Assembly.GetExecutingAssembly().GetName().Name + ";component/" + "Ressourcen/Button_Red.png", …Run Code Online (Sandbox Code Playgroud) 谁能帮我。我不明白如何将BitmapImage或IRandomAccessStream转换为字节数组。我尝试:
foreach (StorageFile file in files)
{
BitmapImage src = new BitmapImage();
using (IRandomAccessStream stream = await file.OpenReadAsync())
{
await src.SetSourceAsync(stream);
WriteableBitmap bitMap = new WriteableBitmap(src.PixelWidth, src.PixelHeight);
await bitMap.SetSourceAsync(stream);
}
}
Run Code Online (Sandbox Code Playgroud)
然后我有WriteableBitmap并尝试以下方法:
private byte[] ImageToByeArray(WriteableBitmap wbm)
{
using (Stream stream = wbm.PixelBuffer.AsStream())
using (MemoryStream memoryStream = new MemoryStream())
{
stream.CopyTo(memoryStream);
return memoryStream.ToArray();
}
}
Run Code Online (Sandbox Code Playgroud)
但这对我不起作用;(
这是续集
所以现在我有一个来自流的 BitmapImage 。简而言之,我想将 BitmapImage 调整为所需的大小。
我找到了大量关于如何从文件系统上的图像调整大小的代码,但没有找到关于如何从现有的 BitmapImage 调整大小的代码
位图压缩过程需要太多时间。我该如何解决这个问题?
在活动中:
icon= BitmapFactory.decodeResource(getResources(),R.mipmap.image);
Run Code Online (Sandbox Code Playgroud)
在回调类中:
synchronized (holder) {
stream = new ByteArrayOutputStream();
Log.d("LIFE_CYCLE", "settingImage 1=" + System.currentTimeMillis());
icon.compress(Bitmap.CompressFormat.PNG, 100, stream);
Log.d("LIFE_CYCLE", "settingImage 2=" + System.currentTimeMillis());
byteArray = stream.toByteArray();
b = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
if(mWidth<mHeight){
icon= Bitmap.createScaledBitmap(b, (int)(mWidth*0.75), (int)(mWidth*0.75), false);
}
else{
icon= Bitmap.createScaledBitmap(b, (int)(mHeight*0.75), (int)(mHeight*0.75), false);
}
canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
canvas.drawBitmap(icon, ((mWidth)-icon.getWidth())/2, (mHeight-icon.getHeight())/2, new Paint());
draw_target(canvas);
}
Run Code Online (Sandbox Code Playgroud)
此行大约需要 2 秒:
icon.compress(Bitmap.CompressFormat.PNG, 100, stream);
Run Code Online (Sandbox Code Playgroud)
PS我的图像是部分透明的,所以我需要使用.PNG而不是.JPG