标签: bitmapimage

从Scheduled Agent保存图像时System.UnauthorizedAccessException

我正在构建一个WP8应用程序,它使用来自Internet的图像来更改锁定屏幕的背景.我按照预定代理和锁屏的教程,但我有一个问题.

当我尝试从计划代理下载新的背景图像时,我得到:

+       $exception  {System.UnauthorizedAccessException: Invalid cross-thread access.
   at MS.Internal.XcpImports.CheckThread()
   at System.Windows.DependencyObject..ctor(UInt32 nativeTypeIndex, IntPtr constructDO)
   at System.Windows.Media.Imaging.BitmapImage..ctor()
   at TileLockAgent.ScheduledAgent.lockScreenClient_OpenReadCompleted(Object sender, OpenReadCompletedEventArgs e)
   at System.Net.WebClient.OnOpenReadCompleted(OpenReadCompletedEventArgs e)
   at System.Net.WebClient.OpenReadOperationCompleted(Object arg)
   at System.Threading.WaitCallback.Invoke(Object state)
   at System.Threading.QueueUserWorkItemCallback.WaitCallback_Context(Object state)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()
   at System.Threading.ThreadPoolWorkQueue.Dispatch()
   at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback()}   System.Exception {System.UnauthorizedAccessException}
Run Code Online (Sandbox Code Playgroud)

代码是:

string fileName;

try
{
    var currentImage = LockScreen.GetImageUri();

    if (currentImage.ToString().EndsWith("_1.jpg"))
    {
        fileName = "LockBackground_2.jpg";
    }
    else
    {
        fileName = …
Run Code Online (Sandbox Code Playgroud)

c# bitmapimage lockscreen unauthorizedaccessexcepti windows-phone-8

4
推荐指数
1
解决办法
2717
查看次数

来自 MemoryStream 的 UWP BitmapImage SetSource 挂起

在我的 UWP 应用程序中,我以字节 [] 的形式将图像存储在 SQLite 数据库中。然后,当我从数据库中检索我的对象时,我将它们绑定到具有 Image 控件的 GridView 数据模板。由于我无法将 Image 的 Source 直接绑定到数组,所以我在对象的类中创建了一个 BitmapImage 属性来将 Image 控件绑定到:

    public BitmapImage Icon
    {
        get
        {
            using (var stream = new MemoryStream(icon))
            {
                stream.Seek(0, SeekOrigin.Begin);
                var img = new BitmapImage();
                img.SetSource(stream.AsRandomAccessStream());
                return img;
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

问题是,我的应用程序挂在 img.SetSource 行上。经过一些试验,我发现这个问题可以用第二个 MemoryStream 来克服:

    public BitmapImage Icon
    {
        get
        {
            using (var stream = new MemoryStream(icon))
            {
                stream.Seek(0, SeekOrigin.Begin);
                var s2 = new MemoryStream();
                stream.CopyTo(s2);
                s2.Position = 0;
                var img = new BitmapImage();
                img.SetSource(s2.AsRandomAccessStream()); …
Run Code Online (Sandbox Code Playgroud)

c# binding bitmapimage uwp

4
推荐指数
1
解决办法
2339
查看次数

*(int*)&data[18] 在这段代码中实际上做了什么?

我遇到了这种在 C++ 中读取 BMP 文件的语法

#include <fstream>
int main() {
    std::ifstream in('filename.bmp', std::ifstream::binary);
    in.seekg(0, in.end);
    size = in.tellg();
    in.seekg(0);
    unsigned char * data = new unsigned char[size];
    in.read((unsigned char *)data, size);

    int width = *(int*)&data[18];
    // omitted remainder for minimal example
}
Run Code Online (Sandbox Code Playgroud)

我不明白线路是什么

int width = *(int*)&data[18];
Run Code Online (Sandbox Code Playgroud)

实际上是在做。为什么从unsigned char *to int, int width = (int)data[18];,的简单转换不起作用?

c++ syntax bmp bitmapimage

4
推荐指数
1
解决办法
271
查看次数

WPF BitmapImage内存问题

我在一个WPF应用程序上工作,该应用程序有多个画布和许多按钮.用户可以加载图像以更改按钮背景.

这是我在BitmapImage对象中加载图像的代码

bmp = new BitmapImage();
bmp.BeginInit();
bmp.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
bmp.CacheOption = BitmapCacheOption.OnLoad;
bmp.UriSource = new Uri(relativeUri, UriKind.Relative);
bmp.EndInit();
Run Code Online (Sandbox Code Playgroud)

并且在EndInit()应用程序的内存增长非常多.

让思考更好(但并没有真正解决问题)的一件事就是增加

bmp.DecodePixelWidth = 1024;
Run Code Online (Sandbox Code Playgroud)

1024 - 我的最大画布大小.但我应该只对宽度大于1024的图像执行此操作 - 那么如何在EndInit()之前获得宽度?

memory wpf bitmapimage

3
推荐指数
1
解决办法
2671
查看次数

WPF BitmapImage序列化/反序列化

我一直在尝试序列化和反序列化BitmapImages.我一直在使用我认为在这个线程中找到的方法:我的byte []错误到WPF BitmapImage转换?

只是为了迭代正在发生的事情,这是我的序列化代码的一部分:

using (MemoryStream ms = new MemoryStream())
                {
                    // This is a BitmapImage fetched from a dictionary.
                    BitmapImage image = kvp.Value; 

                    PngBitmapEncoder encoder = new PngBitmapEncoder();
                    encoder.Frames.Add(BitmapFrame.Create(image));
                    encoder.Save(ms);

                    byte[] buffer = ms.GetBuffer();

                    // Here I'm adding the byte[] array to SerializationInfo
                    info.AddValue((int)kvp.Key + "", buffer);
                }
Run Code Online (Sandbox Code Playgroud)

这是反序列化代码:

// While iterating over SerializationInfo in the deserialization
// constructor I pull the byte[] array out of an 
// SerializationEntry
using (MemoryStream ms = new MemoryStream(entry.Value as byte[]))
                    { …
Run Code Online (Sandbox Code Playgroud)

wpf serialization bitmapimage deserialization

3
推荐指数
1
解决办法
6283
查看次数

如何将BitmapImage转换为Icon?

在我的应用程序中,我必须List<MenuItem>在WCF中向我的shell扩展发送一个.这些MenuItem由图标和标签组成.图标的类型是,System.Drawing.Icon但我必须工作System.Windows.Media.Imaging.BitmapImage.有没有办法转换BitmapImageIcon

c# bitmapimage

3
推荐指数
1
解决办法
4459
查看次数

从base64String加载bitmapImage

我怎样才能加载bitmapImagebase64Stringwindows 8

我试过这个,但我没有成功.它曾经在Windows手机上工作.有什么不同吗?

看起来我必须使用函数setsourceasync.当我使用它时,我需要将参数作为IRandomMemory传递,我无法做到.这该怎么做?

    public static BitmapImage Base64ToImage(string base64String)
    {
        var bitmapImage = new BitmapImage();
        try
        {
            if (!String.IsNullOrEmpty(base64String))
            {
                var imageBytes = Convert.FromBase64String(base64String);
                using (var ms = new MemoryStream(imageBytes, 0, imageBytes.Length))
                {
                    bitmapImage.SetSourcec(ms);
                    return bitmapImage;
                }
            }
        }
        catch (Exception e)
        {

        }

        return null;
    }
Run Code Online (Sandbox Code Playgroud)

c# bitmapimage microsoft-metro windows-8

3
推荐指数
1
解决办法
2398
查看次数

从原始字节创建位图对象

我试图从原始字节创建位图对象时,我的PixelFormatRGB每个样品的8位,这是每个像素3个字节.现在为此我的stide将是宽度的3倍.

但是Bitmap班级总是在寻找乘数为4的乘数.请帮我解决这个问题.如果我给4的乘数图像不正确.

Bitmap im = new Bitmap(MyOBJ.PixelData.Columns, MyOBJ.PixelData.Rows, (MyOBJ.PixelData.Columns*3),
System.Drawing.Imaging.PixelFormat.Format24bppRgb, Marshal.UnsafeAddrOfPinnedArrayElement(images[imageIndex], 0));
Run Code Online (Sandbox Code Playgroud)

c# bitmapimage

3
推荐指数
1
解决办法
5183
查看次数

在WPF中更改BitMapImage的尺寸,以及可以在<Image>元素中放置哪种对象?

我正在尝试使用TreeView元素创建一个资源管理器应用程序,并为树的每个级别提供不同的图标,并按照以下文章进行操作:http://www.codeproject.com/Articles/21248/A-Simple-WPF-Explorer -树

这一切都很好,除了我想要有不同大小的图标.

XAML的Image元素在这里:

<Image Name="img"
       Source="{Binding RelativeSource={RelativeSource Mode=FindAncestor,
       AncestorType={x:Type TreeViewItem}},
       Path=Header,
       Converter={x:Static local:HeaderToImageConverter.Instance}}"
/>
Run Code Online (Sandbox Code Playgroud)

决定返回哪个图标的代码片段在这里:

if ((value as string).Contains(@"\""))
{
    Uri uri = new Uri ("pack://application:,,,/Images/DeployWiz_Network.png");
    BitmapImage source = new BitmapImage(uri);

    return source;
}
Run Code Online (Sandbox Code Playgroud)

如何更改返回图像的尺寸?更改bitmapimage对象的尺寸似乎不起作用.我可以返回哪些其他图像对象作为源?

c# wpf treeview bitmapimage

3
推荐指数
1
解决办法
2万
查看次数

为什么我用这个WinRT代码得到''System.Array'不包含'AsBuffer'的定义?

根据这个,可以使用以下代码将字节数组转换为BitmapImage:

public static async Task<BitmapImage> ByteArrayToBitmapImage(this byte[] byteArray)
{
    if (byteArray != null)
    {
        using (var stream = new InMemoryRandomAccessStream())
        {
            await stream.WriteAsync(byteArray.AsBuffer());
            var image = new BitmapImage();
            stream.Seek(0);
            image.SetSource(stream);
            return image;
        }
    }
    return null;
}
Run Code Online (Sandbox Code Playgroud)

但是,我得到," 'System.Array'不包含'AsBuffer'的定义,并且没有扩展方法'AsBuffer'可以找到类型'System.Array'的第一个参数'(你是否错过了使用指令或者装配参考?) "

是"var stream"赋值太模糊(隐式类型),我需要为"stream"var指定一个特定的数据类型吗?System.Array以外的东西?

也许这一点,来自" Windows Store Apps Succinctly "是一个线索: 缓冲区/字节数组--System.Runtime.InteropServices.WindowsRuntime.WindowsRuntimeBufferExtensions:此类中的扩展方法提供了在.NET字节数组和WinRT缓冲区内容之间移动的方法,这些内容作为IBuffer实现公开.

...但如果是的话,那对我来说不足以让我知道如何处理它.而不是"TMI",它是"NEI"(没有足够的信息).

c# bytearray bitmapimage system.array windows-runtime

3
推荐指数
1
解决办法
2657
查看次数