16

我正在尝试从 to 转换System.Windows.Controls.Imagebyte[]但我不知道 Image 类中的哪个方法可以帮助解决这个问题,顺便说一下,我真的不知道该怎么做,因为在我的 LINQ 模型中,该字段显示为Binary类型,我必须更改如果我想像byte[]类型一样保存它?

我发现这里发布的代码,但没有使用 WPF:

Bitmap newBMP = new Bitmap(originalBMP, newWidth, newHeight);
System.IO.MemoryStream stream = new System.IO.MemoryStream();
newBMP.Save(stream, System.Drawing.Imaging.ImageFormat.Bmp);
PHJProjectPhoto myPhoto = new PHJProjectPhoto {
    ProjectPhoto = stream.ToArray(), // <<--- This will convert your stream to a byte[] 
    OrderDate = DateTime.Now, 
    ProjectPhotoCaption = ProjectPhotoCaptionTextBox.Text,
    ProjectId = selectedProjectId
};
4

7 回答 7

35

真正的解决方案...如果您的 ORM 上的数据库映射字段为 Byte[] / byte[] / Bynary 时,如果想从 System.Windows.Control.Image 保存 jpg 图像

public byte[] getJPGFromImageControl(BitmapImage imageC)
{
       MemoryStream memStream = new MemoryStream();              
        JpegBitmapEncoder encoder = new JpegBitmapEncoder();
        encoder.Frames.Add(BitmapFrame.Create(imageC));
        encoder.Save(memStream);
        return memStream.ToArray();
}

调用为:

getJPGFromImageControl(firmaUno.Source as BitmapImage)

希望有帮助:)

于 2009-02-16T20:33:40.233 回答
12

我不知道您的 Image 是如何声明的,但假设我们有这个 XAML 声明:

<Image x:Name="img">
    <Image.Source>
        <BitmapImage UriSource="test.png" />
    </Image.Source>
</Image>

然后您可以将 test.png 的内容转换为字节数组,如下所示:

var bmp = img.Source as BitmapImage;

int height = bmp.PixelHeight;
int width  = bmp.PixelWidth;
int stride = width * ((bmp.Format.BitsPerPixel + 7) / 8);

byte[] bits = new byte[height * stride];
bmp.CopyPixels(bits, stride, 0);
于 2009-02-16T16:21:42.193 回答
2
public byte[] BufferFromImage(BitmapImage imageSource)
{        
    Stream stream = imageSource.StreamSource;
    byte[] buffer = null;

    if (stream != null && stream.Length > 0)
    {
        using (BinaryReader br = new BinaryReader(stream))
        {
            buffer = br.ReadBytes((Int32)stream.Length);
        }
    }

    return buffer;
}

将是另一种方式,但不同之处在于这比第一个解决方案具有更少的 bytes[x]

于 2009-02-16T19:01:40.063 回答
2
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private void btnBrowse_Click(object sender, RoutedEventArgs e)
    {
        var of = new OpenFileDialog();
        of.Filter = "Image files (*.png;*.jpeg)|*.png;*.jpeg|All files (*.*)|*.*";
        var res = of.ShowDialog();
        if (res.HasValue)
        {
            imgPreview.Source = new BitmapImage(new Uri(of.FileName));

            var t = Utils.ConvertBitmapSourceToByteArray(imgPreview.Source as BitmapSource);
            var d = Utils.ConvertBitmapSourceToByteArray(new BitmapImage(new Uri(of.FileName)));
            var s = Utils.ConvertBitmapSourceToByteArray(imgPreview.Source);
            var enc = Utils.ConvertBitmapSourceToByteArray(new PngBitmapEncoder(), imgPreview.Source);
            //imgPreview2.Source = Utils.ConvertByteArrayToBitmapImage(enc);
            imgPreview2.Source = Utils.ConvertByteArrayToBitmapImage2(enc);
            //var i = 0;


        }
        else
        {
            MessageBox.Show("Select a currect file...");

        }
    }

}

/util.cs/ _ _

public class Utils
{
    public static byte[] ConvertBitmapSourceToByteArray(BitmapEncoder encoder, ImageSource imageSource)
    {
        byte[] bytes = null;
        var bitmapSource = imageSource as BitmapSource;

        if (bitmapSource != null)
        {
            encoder.Frames.Add(BitmapFrame.Create(bitmapSource));

            using (var stream = new MemoryStream())
            {
                encoder.Save(stream);
                bytes = stream.ToArray();
            }
        }

        return bytes;
    }

    public static byte[] ConvertBitmapSourceToByteArray(BitmapSource image)
    {
        byte[] data;
        BitmapEncoder encoder = new JpegBitmapEncoder();
        encoder.Frames.Add(BitmapFrame.Create(image));
        using (MemoryStream ms = new MemoryStream())
        {
            encoder.Save(ms);
            data = ms.ToArray();
        }
        return data;
    }
    public static byte[] ConvertBitmapSourceToByteArray(ImageSource imageSource)
    {
        var image = imageSource as BitmapSource;
        byte[] data;
        BitmapEncoder encoder = new JpegBitmapEncoder();
        encoder.Frames.Add(BitmapFrame.Create(image));
        using (MemoryStream ms = new MemoryStream())
        {
            encoder.Save(ms);
            data = ms.ToArray();
        }
        return data;
    }
    public static byte[] ConvertBitmapSourceToByteArray(Uri uri)
    {
        var image = new BitmapImage(uri);
        byte[] data;
        BitmapEncoder encoder = new JpegBitmapEncoder();
        encoder.Frames.Add(BitmapFrame.Create(image));
        using (MemoryStream ms = new MemoryStream())
        {
            encoder.Save(ms);
            data = ms.ToArray();
        }
        return data;
    }
    public static byte[] ConvertBitmapSourceToByteArray(string filepath)
    {
        var image = new BitmapImage(new Uri(filepath));
        byte[] data;
        BitmapEncoder encoder = new JpegBitmapEncoder();
        encoder.Frames.Add(BitmapFrame.Create(image));
        using (MemoryStream ms = new MemoryStream())
        {
            encoder.Save(ms);
            data = ms.ToArray();
        }
        return data;
    }

    public static BitmapImage ConvertByteArrayToBitmapImage(Byte[] bytes)
    {
        var stream = new MemoryStream(bytes);
        stream.Seek(0, SeekOrigin.Begin);
        var image = new BitmapImage();
        image.BeginInit();
        image.StreamSource = stream;
        image.EndInit();
        return image;
    }
}
于 2016-07-16T17:10:34.230 回答
2

我喜欢命名空间中的编码器和解码器:System.Windows.Media.Imaging

public static class Extensions {        

    public static byte[] ToByteArray(this BitmapSource bitmapSource) {

        var encoder = new JpegBitmapEncoder();
        encoder.Frames.Add(BitmapFrame.Create(bitmapSource));

        using (var stream = new MemoryStream()) {
            encoder.Save(stream);
            return stream.ToArray();
        }
    }

    public static BitmapSource ToBitmapSource(this byte[] bytes) {

        using (var stream = new MemoryStream(bytes)) {
            var decoder = new JpegBitmapDecoder(stream, BitmapCreateOptions.None, BitmapCacheOption.Default);
            return decoder.Frames.First();
        }
    }
}

你可以像这样使用它:

var bytes = bitmapSource.ToByteArray();

或者像这样:

var bitmapSource = bytes.ToBitmapSource();
于 2017-08-14T23:15:01.447 回答
1

这对我有用:

MemoryStream stream = (MemoryStream)bitmapImage.StreamSource;
byte[] data = stream.ToArray();
于 2015-05-06T06:48:42.483 回答
1

您还可以使用 BitmapSources 的 CopyPixels 方法

int stride = snapshot.PixelWidth * (snapshot.Format.BitsPerPixel / 8);
byte[] data = new byte[stride * snapshot.PixelHeight];
snapshot.CopyPixels(data, stride, 0);
var memoryStream = new MemoryStream(data);
于 2016-02-02T16:19:35.170 回答