【问题标题】:Save jpg data from database to a jpg file将 jpg 数据从数据库保存到 jpg 文件
【发布时间】:2013-07-01 15:33:23
【问题描述】:

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

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

【问题讨论】:

    标签: sql-server jpeg


    【解决方案1】:

    您可以在 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();
    

    帮助函数如下 私有静态字节[] InkToBytes(字符串 inkStrokes,字符串编码器类型,浮点 x,浮点 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;
        }
    

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

    【讨论】:

    • 您从哪里获得 Stroke 课程?似乎不是来自 Microsoft.Ink 的那个。
    • 或者来自 System.Windows.Ink 的那个。上面没有我可以看到的 Text 属性或采用整数的构造函数。这看起来很有希望,但我似乎无法弄清楚那部分。
    猜你喜欢
    • 2011-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-18
    • 2011-02-14
    • 2021-03-13
    相关资源
    最近更新 更多