Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WPF图像到字节[]_C#_Wpf_Image_Bytearray - Fatal编程技术网

C# WPF图像到字节[]

C# WPF图像到字节[],c#,wpf,image,bytearray,C#,Wpf,Image,Bytearray,我正在尝试从System.Windows.Controls.Image转换为byte[],我不知道Image类中的哪个方法可以在这个场景中有所帮助,顺便说一下,我真的不知道该怎么办,因为在我的LINQ模型中,字段显示为Binary类型,如果我想像字节一样保存它,我必须更改它[]类型 我在这里找到了发布的代码,但没有使用WPF: Bitmap newBMP = new Bitmap(originalBMP, newWidth, newHeight); System.IO.MemoryStream

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

我在这里找到了发布的代码,但没有使用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
};
Bitmap newBMP=新位图(原始bmp、新宽度、新高度);
System.IO.MemoryStream stream=新的System.IO.MemoryStream();
保存(stream、System.Drawing.Imaging.ImageFormat.Bmp);
PHJProjectPhoto myPhoto=新的PHJProjectPhoto{

ProjectPhoto=stream.ToArray(),//我不知道您的映像是如何声明的,但是假设我们有这个XAML声明:

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

这可能是另一种方式,但不同之处在于,如果要在ORM上的数据库映射字段为Byte[]/Byte[]/Bynary时从System.Windows.Control.Image保存jpg图像,则此解决方案的字节数[x]比第一个解决方案的字节数少

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)
希望有帮助:)

这对我很有用:

MemoryStream stream = (MemoryStream)bitmapImage.StreamSource;
byte[] data = stream.ToArray();

您还可以使用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);
/util.cs/


我喜欢名称空间中的编码器和解码器:

您可以这样使用它:

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);
var bytes = bitmapSource.ToByteArray();
var bitmapSource = bytes.ToBitmapSource();
或者像这样:

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);
var bytes = bitmapSource.ToByteArray();
var bitmapSource = bytes.ToBitmapSource();

我的xaml是:Im在codebehind中设置源代码,那么哪个更好?令人惊讶的是保存:0x000000000000000000000000000000000000000000000000000000000000000…:/我更喜欢这种方法,因为我们可以将任何文件类型保存到byteArray:)我想这取决于您如何初始化Image。如果您将BitmapImage StreamSource设置为空。我正在将Image.Source设置为BitmapImage并获取空BitmapImage,我也尝试转换为BitmapImage,解决方法是什么?它似乎无效,但我到处都可以看到此解决方案,包括。是的…但速度相当慢,470毫秒以上…:(
var bitmapSource = bytes.ToBitmapSource();