【问题标题】:how to convert FormatConvertedBitmap to Stream in C#如何在 C# 中将 FormatConvertedBitmap 转换为 Stream
【发布时间】:2013-01-22 05:35:05
【问题描述】:

我是 C# 新手。

如前所述,我想将FormatConvertedBitmap 转换为Stream。 我找不到任何方法。我想将 FormatConvertedBitmap 保存或写入文件,以便将其作为 Stream 读取,但甚至找不到将其写入文件的方法。

有人可以帮助我吗:

  1. 将 FormatConvertedBitmap 转换为流 或者
  2. 将 FormatConvertedBitmap 写入文件,然后将其作为 Stream 读取。

公开流图片

    {
        get
    {//some condition
                if (_image != null)
                {
                    _image.Seek(0, SeekOrigin.Begin);

                    BitmapImage bmp = new BitmapImage();
                    MemoryStream stream = (MemoryStream)_image;
                    bmp.BeginInit();
                    bmp.StreamSource = stream;
                    bmp.CacheOption = System.Windows.Media.Imaging.BitmapCacheOption.OnLoad;
                    bmp.EndInit();
                    var grayBitmapSource = new FormatConvertedBitmap();
                    grayBitmapSource.BeginInit();
                    grayBitmapSource.Source = bmp;
                    grayBitmapSource.DestinationFormat = PixelFormats.Gray32Float;
                    grayBitmapSource.EndInit();
                    Stream st=new MemoryStream();
                    //File.WriteAllBytes(Path.GetTempPath(),grayBitmapSource);
                    return grayBitmapSource;
                }

在上面的代码中,我从服务器获取 Image as Stream 然后在某些情况下我将其转换为 grayScale image 。但现在我们应该返回一个 Stream,最后我们有了 FormatConvertedBitmap。

#####编辑
public Stream Image
        {
            get
            {
                //some condition

                            if (_image != null)
                            {
                                _image.Seek(0, SeekOrigin.Begin);
                                BitmapImage bmp = new BitmapImage();
                                MemoryStream stream = (MemoryStream)_image;
                                bmp.BeginInit();
                                bmp.StreamSource = stream;
                                bmp.CacheOption = System.Windows.Media.Imaging.BitmapCacheOption.OnLoad;
                                bmp.EndInit();
                                var grayBitmapSource = new FormatConvertedBitmap();
                                grayBitmapSource.BeginInit();
                                grayBitmapSource.Source = bmp;
                                grayBitmapSource.DestinationFormat = PixelFormats.Gray32Float;
                                grayBitmapSource.EndInit();
                                int bytePerPixel = grayBitmapSource.Format.BitsPerPixel / 8;
                                int width = grayBitmapSource.PixelWidth;
                                int height = grayBitmapSource.PixelHeight;
                                int stride = width * bytePerPixel;
                                byte[] resultLine = new byte[height * stride];
                                grayBitmapSource.CopyPixels(resultLine, stride, 0);
                                MemoryStream ms = new MemoryStream(resultLine);
                                return ms;

【问题讨论】:

    标签: c# type-conversion file-handling


    【解决方案1】:

    您可以使用 CopyPixels 方法将 FormatConvertedBitmap 的内容写入字节数组。

    然后,您可以使用该字节数组来初始化 MemoryStream 或将它们存储到带有 BmpBitmapEncoder 或其他 BitmapEncoder 的文件中。

    【讨论】:

    • thanx ,如果您也为 CopyPixels 编写代码,我将不胜感激。我尝试了没有产生灰色效果的代码(有问题的编辑),尽管当我尝试其他项目时使图像变灰的代码工作正常。
    • 我不知道如何使用 copyPixels,所以我在某个地方找到了代码,所以我不确定 Strid 值和全部
    • @SohailAnwerFaruqui 步幅是图像每行的字节数(因此,它看起来不错)。关于灰色效果,我不知道……我猜可能是你解释图像的时候,因为你必须记住它有 Gray32Float 像素格式。
    • @SohailAnwerFaruqui 如果您在步幅方面遇到问题,希望这个答案能帮助您:stackoverflow.com/questions/3881857/… 此外,您可以在这里找到一些相关代码:wrb.home.xs4all.nl/Articles_2010/Article_WPFImageTIFF_01.htm(在该代码中,他们将 7 添加到每像素位数,所以他们通过四舍五入将步幅对齐到 8 的倍数)。
    猜你喜欢
    • 1970-01-01
    • 2023-03-15
    • 2021-12-05
    • 2023-03-31
    • 1970-01-01
    • 2010-09-07
    • 2018-01-15
    • 2013-09-23
    • 1970-01-01
    相关资源
    最近更新 更多