相关疑难解决方法(0)

从System.Drawing.Bitmap加载WPF BitmapImage

我有一个a的实例,System.Drawing.Bitmap并希望以一种形式将它提供给我的WPF应用程序System.Windows.Media.Imaging.BitmapImage.

对此最好的方法是什么?

c# wpf bitmap

217
推荐指数
8
解决办法
21万
查看次数

从MemoryStream png,gif创建WPF BitmapImage

我在创建一个BitmapImage来自MemoryStreamweb请求的png和gif字节时遇到了一些麻烦.这些字节似乎可以很好地下载,并且BitmapImage创建的对象没有问题,但是图像实际上并没有在我的UI上呈现.仅当下载的图像是png或gif类型时才会出现此问题(适用于jpeg).

以下是演示此问题的代码:

var webResponse = webRequest.GetResponse();
var stream = webResponse.GetResponseStream();
if (stream.CanRead)
{
    Byte[] buffer = new Byte[webResponse.ContentLength];
    stream.Read(buffer, 0, buffer.Length);

    var byteStream = new System.IO.MemoryStream(buffer);

    BitmapImage bi = new BitmapImage();
    bi.BeginInit();
    bi.DecodePixelWidth = 30;
    bi.StreamSource = byteStream;
    bi.EndInit();

    byteStream.Close();
    stream.Close();

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

为了测试Web请求是否正确获取字节,我尝试了以下操作,将字节保存到磁盘上的文件,然后使用a UriSource而不是a 加载图像StreamSource,它适用于所有图像类型:

var webResponse = webRequest.GetResponse();
var stream = webResponse.GetResponseStream();
if (stream.CanRead)
{
    Byte[] buffer = new Byte[webResponse.ContentLength];
    stream.Read(buffer, 0, buffer.Length);

    string fName = "c:\\" + …
Run Code Online (Sandbox Code Playgroud)

wpf memorystream image webrequest bitmapimage

28
推荐指数
2
解决办法
6万
查看次数

IO.Stream到WPF中的图像

我试图从仅资源的DLL文件中读取图像.我能够读取图像名称和图像字节,但如何将Image控件设置为流缓冲区?在Windows窗体中,我知道我可以使用这个:

pictureBox1.Image=new System.Drawing.Bitmap(IOStream);
Run Code Online (Sandbox Code Playgroud)

由于wpf中没有Drawing命名空间,我怎样才能实现相同的功能呢?

c# wpf

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

修改数组后,将字节数组转换为c#中的Image

我正在尝试将字节[]转换为C#中的图像.我知道这个问题已经在不同的论坛上提出过.但是他们给出的答案都没有帮助我.给出一些上下文=我打开一个图像,将其转换为byte [].我加密了byte [].最后我仍然有字节[]但它已被修改为ofc.现在我想再次显示它.byte []本身由6559个字节组成.我尝试通过以下方式转换它:

 public Image byteArrayToImage(byte[] byteArrayIn)
    {
        MemoryStream ms = new MemoryStream(byteArrayIn);
        Image returnImage = Image.FromStream(ms);
        return returnImage;
    }
Run Code Online (Sandbox Code Playgroud)

我收到此错误:参数无效.

通过在List上使用.toArray()构造字节数组

List<byte> encryptedText = new List<byte>();    
pbEncrypted.Image = iConverter.byteArrayToImage(encryptedText.ToArray())
Run Code Online (Sandbox Code Playgroud)

;

谁能帮我?我忘了某种格式或什么吗?

必须转换为图像的字节: 替代文字

 private void executeAlgoritm(byte[] plainText)
    {

        // Empty list of bytes
        List<byte> encryptedText = new List<byte>();

        // loop over all the bytes in the original byte array gotten from the image
        foreach (byte value in plainText)
        {
            // convert it to a bitarray
            BitArray myBits = new …
Run Code Online (Sandbox Code Playgroud)

c# byte image

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

创建BitmapImage会抛出缺少的键异常

我有一个视图,显示带有标题和评论的图像.当我加载现有图像时,我使用此代码:

        this.ArtifactIdentity = image.ArtifactIdentity;
        this.Comment = image.Comment;
        this.Name = image.Name;
        this.MediaIdentity = image.MediaIdentity;
        this.ImageArray = image.Image;
        Image = new BitmapImage();
        Image.BeginInit();
        Image.CacheOption = BitmapCacheOption.None;
        Image.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
        Image.StreamSource = new MemoryStream(this.ImageArray);
        Image.EndInit();
Run Code Online (Sandbox Code Playgroud)

当我执行EndInit()时,它会抛出异常缺失参数键.堆栈跟踪显示:

  at System.Collections.Hashtable.ContainsKey(Object key)
  at System.Collections.Hashtable.Contains(Object key)
  at System.Windows.Media.Imaging.ImagingCache.RemoveFromCache(Uri uri, Hashtable table)
  at System.Windows.Media.Imaging.BitmapImage.FinalizeCreation()
  at System.Windows.Media.Imaging.BitmapImage.EndInit()
Run Code Online (Sandbox Code Playgroud)

所以任何人都可以告诉我为什么我在使用代码时遇到此异常我已经看到许多其他人成功使用但我得到这个异常??? 我很茫然!

wpf c#-4.0

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

标签 统计

wpf ×4

c# ×3

image ×2

bitmap ×1

bitmapimage ×1

byte ×1

c#-4.0 ×1

memorystream ×1

webrequest ×1