图像大小调整 - 值不能为空

本文关键字:不能 调整 图像 | 更新日期: 2023-09-27 18:31:29

好的,这就是我得到的:

几行代码,选择/拍照并将其上传到我的服务器:

System.Windows.Media.Imaging.BitmapImage bmp = new System.Windows.Media.Imaging.BitmapImage();
        bmp.SetSource(e.ChosenPhoto);
        image1.Source = bmp;
        BitmapImage bitmapImage = bmp;
        var largest = Math.Max(bitmapImage.PixelWidth, bitmapImage.PixelHeight);
        var ratio = largest / 1024;
        var width = bitmapImage.PixelWidth / ratio;
        var height = bitmapImage.PixelHeight / ratio;
        WriteableBitmap wb = new WriteableBitmap(bitmapImage);
        Stream str = null; 
        wb.SaveJpeg(str, width, height, 0, 75);
        byte[] sbytedata = ReadToEnd(str);
        string s = EncodeTo64(sbytedata.ToString());
        WebClient wc = new WebClient();
        Uri u = new Uri("//something ;)//");
        wc.OpenWriteCompleted+=new OpenWriteCompletedEventHandler(wc_OpenWriteCompleted);
        wc.OpenWriteAsync(u, "POST", sbytedata);

而且..它不起作用:异常 - 屏幕

另外,你还需要ReadToEnd()函数。这是:

    public static byte[] ReadToEnd(System.IO.Stream stream)
{
    long originalPosition = stream.Position;
    stream.Position = 0;
    try
    {
        byte[] readBuffer = new byte[4096];
        int totalBytesRead = 0;
        int bytesRead;
        while ((bytesRead = stream.Read(readBuffer, totalBytesRead, readBuffer.Length - totalBytesRead)) > 0)
        {
            totalBytesRead += bytesRead;
            if (totalBytesRead == readBuffer.Length)
            {
                int nextByte = stream.ReadByte();
                if (nextByte != -1)
                {
                    byte[] temp = new byte[readBuffer.Length * 2];
                    Buffer.BlockCopy(readBuffer, 0, temp, 0, readBuffer.Length);
                    Buffer.SetByte(temp, totalBytesRead, (byte)nextByte);
                    readBuffer = temp;
                    totalBytesRead++;
                }
            }
        }
        byte[] buffer = readBuffer;
        if (readBuffer.Length != totalBytesRead)
        {
            buffer = new byte[totalBytesRead];
            Buffer.BlockCopy(readBuffer, 0, buffer, 0, totalBytesRead);
        }
        return buffer;
    }
    finally
    {
        stream.Position = originalPosition;
    }
}

有什么想法吗?

图像大小调整 - 值不能为空

更改

Stream str = null; 

自:

Stream str = new MemoryStream();

请记住 - 调试器会将下一条语句显示为导致异常的语句之后的语句。 所以你实际上是在你看到的语句之前死在语句上。

好吧,

它似乎在说Stream str = null;错了,您需要实例化它,即 SaveToJpeg 写入现有流而不是创建一个流并写入它。