我有一个很长的原始列,其中包含一个tiff图像,该图像由oracle窗体应用程序保存,我试图通过c#检索该图像,并且没有运气就保存了该图像,该图像是有效的,但是它显示的图像很烂。 / p>
数据库列定义
SIGNATURE NOT NULL LONG RAW()
C#代码
内部无效保存(字符串帐户) { var commonAccount = new List();
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();
}
我建立了一个新的oracle表单并将图像与列绑定,然后正确显示了,知道吗?
编辑: 我发现Oracle数据库中的映像保存为Big-Endian字节顺序
答案 0 :(得分:1)
经过几天的了解并找到解决方案,以下解决了我的问题,下面的代码转换为另一种图像编码也转换为little-endian
请注意,该代码正在使用BitMiracle.LibTiff库,
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;
}