【问题标题】:.NET Streams with Dynamics Nav RTC and C#.NET 流与 Dynamics Nav RTC 和 C#
【发布时间】:2016-02-25 17:37:26
【问题描述】:

目前,我仍在使用带有 COM 类的 C# 库通过 Interop 使用 Nav2009R2 经典版。我的大约 20000 张图片是 Jpeg,没有存储在 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 Server 上运行。

这意味着,我必须将此代码实现为纯 .Net 类中的方法,而不需要 Interop-Interface。 不幸的是,我不知道如何将流从 Nav 传递给 .Net 方法,因此可以像上面的示例一样读取和写入。使用通用“对象”类型并将其转换为 IStream 仍然是最好的方法吗?

提前致谢

皮迪

【问题讨论】:

  • 我想将图像转换为位图,因此我将它们存储在一个 blob 文件中。你能否提供更多关于你是如何完成的描述

标签: .net stream nav microsoft-dynamics


【解决方案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 格式发送给你。

干杯!

【讨论】:

  • 非常感谢您如此快速而周到的回复。老实说,乍一看我已经被你的代码弄糊涂了——没有 C#。作为 Nav classic 的用户,它对我来说并不常见,可以在任何 Nav 代码中直接使用 .Net 类。这当然会让一切变得更容易,并且由于大部分“工作”将在 .Net 类中处理,这也不应该是性能问题。如果你能把 txt 对象发给我,那就太好了。非常感谢。皮迪
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-14
  • 1970-01-01
  • 2018-02-09
  • 1970-01-01
相关资源
最近更新 更多