【问题标题】:SSIS setting column with data in Script ComponentSSIS 设置列与脚本组件中的数据
【发布时间】:2015-01-11 10:27:37
【问题描述】:

我有一个脚本组件,它获取图像,使用 c# 代码使其更小,然后尝试将 byte[] 结果设置为列。此时我得到一个错误:

在 Microsoft.SqlServer.Dts.Pipeline.Wrapper.IDTSManagedComponentWrapper100.AddBLOBData(IDTSBuffer100 pIDTSBuffer, Int32 hRow, Int32 hCol, Byte[]& ppsaData) 在 Microsoft.SqlServer.Dts.Pipeline.PipelineBuffer.AddBlobData(Int32 columnIndex, Byte[] 数据)在 ScriptMain.Input0_ProcessInputRow(Input0Buffer Row) 在 c:\Users\e032955999\AppData\Local\Temp\vsta\65b7c0a9d6444e7f987741e111376505\main.cs:line 127 在 UserComponent.Input0_ProcessInput(Input0Buffer 缓冲区) 中 c:\Users\e032955999\AppData\Local\Temp\vsta\65b7c0a9d6444e7f987741e111376505\ComponentWrapper.cs:line 36 在 UserComponent.ProcessInput(Int32 InputID, String InputName, PipelineBuffer 缓冲区,OutputNameMap OutputMap) 中 c:\Users\e032955999\AppData\Local\Temp\vsta\65b7c0a9d6444e7f987741e111376505\ComponentWrapper.cs:line 27 在 Microsoft.SqlServer.Dts.Pipeline.ScriptComponent.ProcessInput(Int32 InputID,PipelineBuffer 缓冲区)在 Microsoft.SqlServer.Dts.Pipeline.ScriptComponentHost.ProcessInput(Int32 inputID,PipelineBuffer 缓冲区)

代码:

    public override void Input0_ProcessInputRow(Input0Buffer Row)
{

    //Row.BigImage = Row.Image;
    Row.FilePath = Row.Path;
    byte[] arr = MakeSmallImage(Row.Image);
    Row.SmallImage.ResetBlobData();
    Row.BigImage.AddBlobData(arr);
    Row.SmallImage.AddBlobData(arr);

}



private byte[] MakeSmallImage(Microsoft.SqlServer.Dts.Pipeline.BlobColumn blobColumn)
{
    const int iSmallImageMaxSize = 15 * 1024;
    const int iSmallImageMinSize = 10 * 1024;

    byte[] BytePicture = blobColumn.GetBlobData(0, (int)blobColumn.Length);//Get the picture bytes from the blob.
    byte[] SmallPicture = null;

    if (BytePicture.Length < iSmallImageMaxSize)
    {

        using (MemoryStream oOriginalPictureStream = new MemoryStream(BytePicture))
        {
            Image oPicture;

            int sourceX = 0;
            int sourceY = 0;

            int destX = 0;
            int destY = 0;

            oPicture = Image.FromStream(oOriginalPictureStream);
            Size OriginalPictureSize = oPicture.Size;

            int CompressedPictureBytes = BytePicture.Length;
            MemoryStream oCompressedPictureStream = new MemoryStream(iSmallImageMaxSize);
            Size CompressedPictureSize = OriginalPictureSize;

            double Ratio = ((double)OriginalPictureSize.Width) / OriginalPictureSize.Height;
            Size CompressedPictureMinSize = new Size((int)(120 * Ratio), 120);
            Size CompressedPictureMaxSize = OriginalPictureSize;

            int Count = 1;
            do
            {
                oCompressedPictureStream.Position = 0;
                switch (Count)
                {
                    case 1:
                        Count++;
                        break;
                    case 2:
                        Ratio = Math.Sqrt(((double)iSmallImageMaxSize) / CompressedPictureBytes);
                        CompressedPictureSize.Width = (int)(CompressedPictureSize.Width * Ratio);
                        CompressedPictureSize.Height = (int)(CompressedPictureSize.Height * Ratio);
                        Count++;
                        break;
                    default:
                        if (iSmallImageMaxSize > CompressedPictureBytes)
                        {
                            CompressedPictureMinSize = CompressedPictureSize;
                        }
                        else
                        {
                            CompressedPictureMaxSize = CompressedPictureSize;
                        }
                        CompressedPictureSize = (CompressedPictureMaxSize - CompressedPictureMinSize);
                        CompressedPictureSize.Width = CompressedPictureSize.Width / 2;
                        CompressedPictureSize.Height = CompressedPictureSize.Height / 2;
                        CompressedPictureSize += CompressedPictureMinSize;
                        break;
                }

                Bitmap CompressedPicture = new Bitmap(CompressedPictureSize.Width, CompressedPictureSize.Height,
                                                      PixelFormat.Format24bppRgb);
                CompressedPicture.SetResolution(Math.Min(oPicture.HorizontalResolution, 72),
                                                Math.Min(oPicture.VerticalResolution, 72));

                Graphics grPhoto = Graphics.FromImage(CompressedPicture);
                grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic;

                grPhoto.DrawImage(oPicture,
                                  new Rectangle(destX, destY, CompressedPictureSize.Width,
                                                CompressedPictureSize.Height),
                                  new Rectangle(sourceX, sourceY, OriginalPictureSize.Width,
                                                OriginalPictureSize.Height),
                                  GraphicsUnit.Pixel);

                grPhoto.Dispose();
                CompressedPicture.Save(oCompressedPictureStream, System.Drawing.Imaging.ImageFormat.Jpeg);

                CompressedPicture.Dispose();
                CompressedPictureBytes = (int)oCompressedPictureStream.Position;
                oCompressedPictureStream.SetLength(CompressedPictureBytes);
            } while (((CompressedPictureMaxSize - CompressedPictureMinSize).Width > 50) &&
                     ((CompressedPictureBytes > iSmallImageMaxSize) ||
                      (CompressedPictureBytes < iSmallImageMinSize)));
            oPicture.Dispose();
            //SmallPicture = new byte[oCompressedPictureStream.Length];
            SmallPicture = oCompressedPictureStream.ToArray();
            oCompressedPictureStream.Dispose();
        }

        //oDAL.UploadFileToDatabase(SmallPicture, BytePicture, lTmunaID);

    }

    return SmallPicture;
}

【问题讨论】:

  • MakeSmallImage 中,如果您使用 try/catch 块包装该 if 语句,错误会消失吗?
  • 我把它包起来了。错误并没有消失。

标签: c# .net ssis


【解决方案1】:

也许是因为null?当BytePicture.Length &lt; iSmallImageMaxSize 条件为假时,MakeSmallImage 方法将返回 null
BlobColumn 类具有 SetNull 方法,因此您应该使用它来清空 blob。

【讨论】:

  • 就是这样。完美!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多