c#无法从Oracle long raw读取tiff图像

c#无法从Oracle long raw读取tiff图像,c#,oracle,oracleforms,C#,Oracle,Oracleforms,我有一个很长的raw列,其中包含一个由oracle表单应用程序保存的tiff图像,我正试图用c#检索图像并保存它,但没有运气,图像是有效的,但它显示的是蹩脚的图形 数据库列定义 SIGNATURE NOT NULL LONG RAW() C#代码 内部作废保存(字符串帐户) { var commonAccount=新列表() 保存的图像看起来像 我创建了新的oracle表单,并将图像与列绑定,它显示正确,你知道吗? 编辑: 我发现Oracle数据库中的图像保存为Big-Endi

我有一个很长的raw列,其中包含一个由oracle表单应用程序保存的tiff图像,我正试图用c#检索图像并保存它,但没有运气,图像是有效的,但它显示的是蹩脚的图形

数据库列定义

SIGNATURE     NOT NULL LONG RAW()  
C#代码

内部作废保存(字符串帐户) { var commonAccount=新列表()

保存的图像看起来像

我创建了新的oracle表单,并将图像与列绑定,它显示正确,你知道吗?

编辑:
我发现Oracle数据库中的图像保存为Big-Endian字节顺序

经过几天的理解和解决方案,下面的代码解决了我的问题,下面的代码转换为另一种类型的图像编码也转换为little-Endian

请注意,代码正在使用库

私有字符串GetBase64Data(字节[]图像) { var data=string.Empty; 使用(MemoryStream ms=新的MemoryStream(图像)) { 使用(Tiff tif=Tiff.ClientOpen(“内存中”、“r”、ms、新的TiffStream())) { //找到图像的宽度和高度 FieldValue[]value=tif.GetField(tiftTag.IMAGEWIDTH); int width=值[0]。ToInt(); 值=tif.GetField(TiffTag.IMAGELENGTH); int height=值[0]。ToInt(); //将图像读入内存缓冲区 int[]光栅=新int[高度*宽度]; 如果(!tif.ReadRGBAImage(宽度、高度、光栅)) { 返回数据; } 使用(位图bmp=新位图(宽度、高度、像素格式.Format24bppRgb)) { 矩形rect=新矩形(0,0,bmp.Width,bmp.Height); BitmapData bmpdata=bmp.LockBits(rect,ImageLockMode.ReadWrite,PixelFormat.Format24bppRgb); 字节[]位=新字节[bmpdata.Stride*bmpdata.Height]; 对于(int y=0;y>16)和0xff); 位[bitsofset++]=(字节)((rgba>>8)和0xff); 位[bitsofset++]=(字节)(rgba和0xff); } } System.Runtime.InteropServices.Marshal.Copy(bits,0,bmpdata.Scan0,bits.Length); bmp.UnlockBits(bmpdata); MemoryStream ims=新的MemoryStream(); 保存(ims,ImageFormat.bmp); 数据=Convert.tobase64字符串(ims.ToArray()); } } } 返回数据; }
我无法重现此错误。我所做的是:1)使用单个字段创建了一个表;2)使用图像数据插入了一行;2)根据您的代码创建。代码工作正常,正确的图像保存在
D:\image.tiff
中。我做错了什么?您能帮我重现您的问题吗?您的示例包括分为两个部分:1)数据库访问;2)字节数组操作。也许,您可以确定其中哪一个有问题。为此,您需要将
imgBytes
的内容与数据库中的实际字节进行比较。如果字节值不匹配,您可以删除位图操作并集中精力访问数据库(一位专家会有帮助的。)您是否通过oracle form将图像保存到oracle?或者从.net方面,我不确定oracle form image组件在将图像保存到数据库时是否对图像进行了一些处理,如压缩?不,我只是使用SQLDeveloper将图像直接插入数据库。然后我能够验证C代码是否正常工作。不确定在Oracle窗体中,检查数据库中实际存储的内容是有意义的
        using (OracleConnection cn = new OracleConnection(ConfigurationManager.ConnectionStrings["-----"].ConnectionString))
        {
            var imgCmd = new OracleCommand("select SIGNATURE, number, code, name  from table_name where number = ***** and code = *****", cn);
            imgCmd.InitialLONGFetchSize = -1;
            cn.Open();

            var reader = imgCmd.ExecuteReader();
            if (reader.Read())
            {
                //var v1 = reader[0];
                var v2 = reader[1].ToString();
                var v3 = reader[2].ToString();
                var v4 = reader[3].ToString();

                OracleBinary imgBinary = reader.GetOracleBinary(0);

                // Get the bytes from the binary obj
                byte[] imgBytes = imgBinary.IsNull ? null : imgBinary.Value;

                var newData = Convert.ToBase64String(imgBytes);


                MemoryStream stream = new MemoryStream();
                stream.Write(imgBytes, 0, imgBytes.Length);
                Bitmap bm = new Bitmap(stream);
                bm.Save("d:\\image.tif", System.Drawing.Imaging.ImageFormat.Tiff);
            }

            reader.Close();
        }
        private string GetBase64Data(byte [] image)
    {
        var data = string.Empty;
        using (MemoryStream ms = new MemoryStream(image))
        {
            using (Tiff tif = Tiff.ClientOpen("in-memory", "r", ms, new TiffStream()))
            {
                // Find the width and height of the image
                FieldValue[] value = tif.GetField(TiffTag.IMAGEWIDTH);
                int width = value[0].ToInt();

                value = tif.GetField(TiffTag.IMAGELENGTH);
                int height = value[0].ToInt();

                // Read the image into the memory buffer
                int[] raster = new int[height * width];
                if (!tif.ReadRGBAImage(width, height, raster))
                {
                    return data;
                }

                using (Bitmap bmp = new Bitmap(width, height, PixelFormat.Format24bppRgb))
                {
                    Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);

                    BitmapData bmpdata = bmp.LockBits(rect, ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
                    byte[] bits = new byte[bmpdata.Stride * bmpdata.Height];

                    for (int y = 0; y < bmp.Height; y++)
                    {
                        int rasterOffset = y * bmp.Width;
                        int bitsOffset = (bmp.Height - y - 1) * bmpdata.Stride;

                        for (int x = 0; x < bmp.Width; x++)
                        {
                            int rgba = raster[rasterOffset++];
                            bits[bitsOffset++] = (byte)((rgba >> 16) & 0xff);
                            bits[bitsOffset++] = (byte)((rgba >> 8) & 0xff);
                            bits[bitsOffset++] = (byte)(rgba & 0xff);
                        }
                    }

                    System.Runtime.InteropServices.Marshal.Copy(bits, 0, bmpdata.Scan0, bits.Length);
                    bmp.UnlockBits(bmpdata);

                    MemoryStream ims = new MemoryStream();
                    bmp.Save(ims, ImageFormat.Bmp);
                    data = Convert.ToBase64String(ims.ToArray());
                }
            }
        }

        return data;
    }