.NET Streams与Dynamics Nav RTC和C#

时间:2016-02-25 17:37:26

标签: .net stream nav microsoft-dynamics

目前我还在使用带有COM类的C#库通过Interop的Nav2009R2 classic。我的大约20000张图片是Jpegs,并没有存储在DBs blob中,而是存储在简单的网络共享中。 要转换为bmp并缩放这些图片,几年前我写了一个小的C#函数,它仍然可以正常工作。它从文件中读取jpeg,转换并缩放它,并将该bmp写入Nav可以访问它的流。在Nav中非常快速,非常稳定且易于使用:

  // just bind "TempRec.BlobField" to the picture control, and that´s it within Nav
  IF ISCLEAR(LocAutImaging) THEN CREATE(LocAutImaging, FALSE, TRUE);
   TempRec.BlobField.CREATEINSTREAM(LocStreamPreviewPic);
   LocAutImaging.ConvertFileToBmpStream(LocStreamPreviewPic, '\\..\..\Picture.jpg', 300, 300);
  CLEAR(LocAutImaging);

使用带有Nav和C#的流有点棘手,但它有效:

    public void ConvertFileToBmpStream(object ObjPictureStream, string StrSourceFile, int NewHeigth, int NewWidth)
    {
        Bitmap MyBitMap = null;
        IStream StmPicStream = ObjPictureStream as IStream;
        MemoryStream ms = new MemoryStream();
        IntPtr rwBytes = Marshal.AllocHGlobal(4);

        Image img = Image.FromFile(StrSourceFile);
        Size MySize = new Size(NewWidth, NewHeigth);
        if (NewHeigth == 0 & NewWidth == 0){ MyBitMap = new Bitmap(img); } else { MyBitMap = new Bitmap(img, MySize); }

        MyBitMap.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
        int StreamLength = Convert.ToInt32(ms.Length);

        byte[] buffer = new byte[StreamLength];
        buffer = ms.ToArray();

        StmPicStream.Write(buffer, StreamLength, rwBytes);
        Marshal.FreeHGlobal(rwBytes);
        ms.Dispose();
        img.Dispose();
        MyBitMap.Dispose();
    }

虽然一切都很好,但我们现在正致力于Nav 2016更新。使用RTC,COM控件仍然可以在客户端使用;但是......没有机会,如果你正在使用溪流!另一方面,您不能使用Interop COM功能在Nav服务器上运行。

这意味着,我必须在没有Interop-Interface的情况下将此代码实现为纯.Net类中的方法。 不幸的是,我不知道如何将一个流从Nav传递到.Net方法,所以它可以像上面的示例一样进行读写。是否仍然是最好的方法,使用一般的“对象”类型并将其转换为IStream?

提前致谢

皮底

1 个答案:

答案 0 :(得分:1)

这是一个使用DotNet调整图像大小的代码

ResizePhoto(Filename : Text;NewFilePath : Text;MaxHeight : Integer)
IF Filename = '' THEN
  IF GUIALLOWED THEN ERROR(NoFilename) ELSE EXIT;

IF NewFilePath = '' THEN BEGIN
  DirectoryInfo := DirectoryInfo.DirectoryInfo(Path.GetDirectoryName(NewFilePath));
  IF NOT DirectoryInfo.Exists THEN
    IF GUIALLOWED THEN ERROR(STRSUBSTNO(NewDirectoryDoesntExists,DirectoryInfo.Name)) ELSE EXIT;
END;

IF MaxHeight = 0 THEN
  IF GUIALLOWED THEN ERROR(NoNewSize) ELSE EXIT;

AuctionSetup.GET;
IF GUIALLOWED THEN Setup.TESTFIELD("Photo Pool Path") 
ELSE IF Setup."Photo Pool Path" = '' THEN ERROR(NoPhotoPoolPath);

Filepath := Path.Combine(Setup."Photo Pool Path",Filename);
IF NewFilePath = '' THEN NewFilePath := Filepath;

Image := Image.FromFile(Filepath);

Pct := MaxHeight / Image.Height;
NewWidth := ROUND(Image.Width * Pct,1);
NewHeight := ROUND(Image.Height * Pct,1);

Bitmap := Bitmap.Bitmap(NewWidth,NewHeight);
Graphics := Graphics.FromImage(Bitmap);
InterpolationMode := InterpolationMode.HighQualityBicubic;
Graphics.InterpolationMode := InterpolationMode;
SmoothingMode := SmoothingMode.HighQuality;
Graphics.SmoothingMode := SmoothingMode;
PixelOffsetMode := PixelOffsetMode.HighQuality;
Graphics.PixelOffsetMode := PixelOffsetMode;
Graphics.DrawImage(Image,0,0,NewWidth,NewHeight);
Graphics.Dispose;
Image.Dispose;

ImageFormat := ImageFormat.Jpeg;
Bitmap.Save(NewFilePath,ImageFormat);

变量:

Name            DataType    Subtype Length
Setup           Record      Setup   
Filepath        Text            
Path            DotNet      System.IO.Path.'mscorlib'   
DirectoryInfo       DotNet      System.IO.DirectoryInfo.'mscorlib'  
Image           DotNet      System.Drawing.Image.'System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'    
Bitmap          DotNet      System.Drawing.Bitmap.'System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'   
Graphics        DotNet      System.Drawing.Graphics.'System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' 
InterpolationMode   DotNet      System.Drawing.Drawing2D.InterpolationMode.'System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'  
SmoothingMode       DotNet      System.Drawing.Drawing2D.SmoothingMode.'System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'  
PixelOffsetMode     DotNet      System.Drawing.Drawing2D.PixelOffsetMode.'System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'    
ImageFormat     DotNet      System.Drawing.Imaging.ImageFormat.'System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'  
Pct         Decimal     
HeightPct       Decimal     
WidthPct        Decimal     
NewWidth        Integer     
NewHeight       Integer     
NewWidthDec     Decimal     
NewHeightDec        Decimal     
DecimalZero     Decimal     

用法:

ConvertFileNameDotNet(FileName : Text;Resolution : Integer;VAR Image : TEMPORARY Record TempBitmapBlob)
IF Resolution = 0 THEN Resolution := 300;
Setup.GET;
Setup.TESTFIELD("Photo Pool Path");

GUID := FORMAT(CREATEGUID);
ConvertedServerTempFilePath := 'C:\TEMP\' + GUID + '_CONVERTED.JPG';
IF EXISTS(ConvertedServerTempFilePath) THEN IF ERASE(ConvertedServerTempFilePath) THEN;
IF NOT EXISTS(Setup."Photo Pool Path" + FileName) THEN EXIT;

ResizePhoto(FileName,ConvertedServerTempFilePath,Resolution);

IF EXISTS(ConvertedServerTempFilePath) THEN BEGIN
  FileManagement.BLOBImportFromServerFile(TempBlob,ConvertedServerTempFilePath);
  Image.Blob := TempBlob.Blob;
  IF EXISTS(ConvertedServerTempFilePath) THEN IF ERASE(ConvertedServerTempFilePath) THEN;
END;

如果你愿意,我可以在txt中发送你的对象。

干杯!