2013-07-03 163 views
0

我想將包含jpeg圖像的字段中的數據保存到實際的文件中,我可以使用繪製編輯器打開該文件。我知道我可以創建一個應用程序來做到這一點在c#中,但我想知道是否有一個簡單的方法來從SQL查詢做到這一點?將jpg數據從數據庫保存到jpg文件

它不需要一次處理所有記錄。我只需要能夠選擇單個記錄並將該記錄的圖像保存到文件中。

回答

0

可以使用的MemoryStream對象在C#作爲以下的 私有靜態字節[] InkToBytes

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(); 

幫助器函數(串inkStrokes,串encoderType,浮子的x,浮子Y) { 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; 
    } 

希望這會有所幫助。 注意:我沒有考慮優化上面的代碼

+0

你從哪裏得到Stroke類?似乎不是來自Microsoft.Ink的人。 – BVernon

+0

或者來自System.Windows.Ink的那個。沒有我可以看到的Text屬性或帶有整數的構造函數。這看起來很有前途,但我似乎無法想出這一部分。 – BVernon