将jpg数据从数据库保存到jpg文件

时间:2013-07-03 19:28:08

标签: sql-server jpeg

我想在包含jpeg图像的字段中获取数据并将其保存到我可以使用绘图编辑器打开的实际文件中。我知道我可以创建一个在c#中执行此操作的应用程序,但我想知道是否有一种快速简便的方法可以从SQL查询中执行此操作?

不需要同时处理所有记录。我只需要能够选择一条记录并将该记录的图像保存到文件中。

1 个答案:

答案 0 :(得分:0)

您可以在C#中使用MemoryStream对象,如下所示

MemoryStream memstmSignature = new MemoryStream(InkToBytes(ds.Tables[1].Rows[0]["SIGNATURE"].ToString(), "image/gif", 0, 0));
Image imaSig = Image.FromStream(memstmSignature);
imaSig.RotateFlip(RotateFlipType.Rotate180FlipX);
imaSig.Save(Path.Combine(this.temporaryFilePath, this.signatureFileName));
memstmSignature.Close();

Helper功能如下         private static byte [] InkToBytes(string inkStrokes,string encoderType,float x,float y)         {             ArrayList strokes = new ArrayList();

        // max size for bit map
        int maxX = 0;
        int maxY = 0;

        // intialize the strokes array with text string
        int strokesCount;
        if (inkStrokes.Length > 0)
        {
            if (inkStrokes.EndsWith(";"))
                inkStrokes = inkStrokes.Remove((inkStrokes.Length - 1), 1);

            char[] strokeSeperator = { ';' };
            string[] strokeArray = inkStrokes.Split(strokeSeperator);
            strokesCount = strokeArray.Length;

            for (int i = 0; i < strokesCount; i++)
            {
                Stroke stroke = new Stroke(50);
                stroke.Text = strokeArray[i];
                strokes.Add(stroke);

                Point[] points = stroke.ToPointArray();
                int len = points.Length;
                foreach (Point point in points)
                {
                    maxX = (point.X > maxX ? point.X : maxX);
                    maxY = (point.Y > maxY ? point.Y : maxY);
                }
            }
        }

        // setup the gdi objects
        Bitmap bitMap = new Bitmap(maxX + 20, maxY + 20); // leave some buffer room
        Graphics graphics = Graphics.FromImage(bitMap);
        Rectangle clientRect = new Rectangle(0, 0, bitMap.Width, bitMap.Height);
        Pen pen = new Pen(Color.Black);
        pen.Width = 2; // matches the client capture

        // draw the bit map
        if (x > 0 && y > 0)
            graphics.DrawImage(bitMap, x, y);
        else
            graphics.DrawImage(bitMap, 0, 0);
        graphics.FillRectangle(Brushes.White, clientRect);
        graphics.DrawRectangle(pen, clientRect);
        strokesCount = strokes.Count;
        for (int j = 0; j < strokesCount; j++)
        {
            Stroke stroke = (Stroke)strokes[j];
            if (stroke != null)
            {
                Point[] points = stroke.ToPointArray();
                int len = points.Length;
                if (len > 1)
                {
                    Point prevPoint = points[0];
                    for (int i = 1; i < len; i++)
                    {
                        graphics.DrawLine(pen, prevPoint.X, prevPoint.Y, points[i].X, points[i].Y);
                        prevPoint = points[i];
                    }
                }
            }
        }

        // create the bytes from the bitmap to be returned
        MemoryStream memStream = new MemoryStream(1000);
        ImageCodecInfo imageCodedInfo = GetEncoderInfo(encoderType);
        bitMap.Save(memStream, imageCodedInfo, null);
        byte[] bytes = memStream.GetBuffer();
        memStream.Close();
        return bytes;
    }

    private static ImageCodecInfo GetEncoderInfo(String mimeType)
    {
        int j;
        ImageCodecInfo[] encoders;
        encoders = ImageCodecInfo.GetImageEncoders();
        for (j = 0; j < encoders.Length; ++j)
        {
            if (encoders[j].MimeType == mimeType)
                return encoders[j];
        }
        return null;
    }

希望这会有所帮助。 注意:我没有考虑优化上面的代码