【问题标题】:asp.net c# convert filestream to imageasp.net c# 将文件流转换为图像
【发布时间】:2012-01-13 09:58:39
【问题描述】:

我有一个文件流,我试图将其转换为图像。我有以下代码

FileStream imageFile = image["file"] as FileStream;

image 是一个包含图像信息的地图。请有人指导我下一步应该做什么。

【问题讨论】:

  • 为什么要复制 image 变量名?我怀疑你有什么会编译。
  • “转换为图像”是什么意思 - 是将其转换为 Bitmap 实例还是将其提供给客户端以响应图像请求?
  • 图像在服务器上,我使用 FileStream 获取它,想法是在网页上显示图像。

标签: c# asp.net filestream


【解决方案1】:

Image.FromStream 将获取一个流并返回一个图像。顺便说一句,如果您使用Bitmap.FromStream 或Image.FromStream,或者实际上任何这些方法,它们都返回Bitmap,因此如果您希望它们都可以从Image 向上转换为Bitmap。

【讨论】:

    【解决方案2】:

    您可以执行以下操作:

            int width = 128;
            int height = width;
            int stride = width / 8;
            byte[] pixels = new byte[height * stride];
    
            // Define the image palette
            BitmapPalette myPalette = BitmapPalettes.Halftone256;
    
            // Creates a new empty image with the pre-defined palette
            BitmapSource image = BitmapSource.Create(
                width,
                height,
                96,
                96,
                PixelFormats.Indexed1,
                myPalette,
                pixels,
                stride);
    
            FileStream stream = new FileStream("new.jpg", FileMode.Create);
            JpegBitmapEncoder encoder = new JpegBitmapEncoder();
            encoder.FlipHorizontal = true;
            encoder.FlipVertical = false;
            encoder.QualityLevel = 30;
            encoder.Rotation = Rotation.Rotate90;
            encoder.Frames.Add(BitmapFrame.Create(image));
            encoder.Save(stream);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-05-12
      • 2021-08-20
      • 1970-01-01
      • 1970-01-01
      • 2022-01-22
      • 2012-08-13
      • 2011-03-30
      • 1970-01-01
      相关资源
      最近更新 更多