修改一下DELPHI ,cxImage控件显示图片异常时候的情况
修改一下DELPHI ,cxImage控件显示图片异常时候的情况
通吃岛-低手哥 发表于2年前
修改一下DELPHI ,cxImage控件显示图片异常时候的情况
  • 发表于 2年前
  • 阅读 98
  • 收藏 0
  • 点赞 1
  • 评论 0

移动开发云端新模式探索实践 >>>   

摘要: 修改一下DELPHI ,cxImage控件显示图片异常时候的情况

客户提出客户端预览图片的时候经常报错,影响操作。经检查是图片异常导致cxImage控件不停的显示异常导致的。
我个人为了省事,直接修改 DevExpress\Library\ExpressEditors5\cxImage.pas 文件

定位到1462行这个函数:procedure TcxImageViewInfo.InternalPaint(ACanvas: TcxCanvas);

增加try except end,省事。。。

 try
    with ACanvas do
    begin
      if not Assigned(Picture) or IsPictureEmpty(Picture) then
      begin
        inherited InternalPaint(ACanvas);
        Brush.Color := BackgroundColor;
        if Caption <> '' then
        begin
          Brush.Style := bsClear;
          Canvas.Font.Assign(Self.Font);
          Canvas.Font.Color := Self.TextColor;
          ACanvas.DrawText(Caption, CR, cxAlignCenter + cxSingleLine);
          Brush.Style := bsSolid;
        end;
        if ShowFocusRect then FocusRect(Canvas, CR);
        Exit;
      end;
      with CR do
      begin
        if Stretch then
        begin
          CalcStretchRect(CR, Picture.Width, Picture.Height, R);
          if Center then
          begin
            OffsetRect(R, (Right - Left - (R.Right - R.Left)) div 2, 0);
            OffsetRect(R, 0, (Bottom - Top - (R.Bottom - R.Top)) div 2);
          end;
        end
        else
        begin
          R := Rect(Left, Top, Left + Picture.Width, Top + Picture.Height);
          if Center then
          begin
            OffsetRect(R, (Right - Left - Picture.Width) div 2, 0);
            OffsetRect(R, 0, (Bottom - Top - Picture.Height) div 2);
          end
          else
            OffsetRect(R, -Self.TopLeft.X, -Self.TopLeft.Y);
        end;
      end;
      SaveRgn := GetClipRegion; // for native mode
      ExcludeClipRect(CR);
      DrawCustomEdit(ACanvas, Self, False, bpsSolid);
      SetClipRegion(SaveRgn, roSet);
      if ShowFocusRect then
      begin
        FocusRect(Canvas, CR);
        InflateRect(CR, -1, -1);
      end;
      SaveRgn := GetClipRegion;
      IntersectClipRect(CR);
      if ShowFocusRect then InflateRect(CR, 1, 1);
      if not Self.Transparent and Picture.Graphic.Transparent then
      begin
        if FTempBitmap = nil then
        begin
          FTempBitmap := TBitmap.Create;
          FTempBitmap.PixelFormat := pfDevice;
        end;
        try
          FTempBitmap.Width := R.Right - R.Left;
          FTempBitmap.Height := R.Bottom - R.Top;
          FTempBitmap.Canvas.Brush.Color := BackgroundColor;
          FTempBitmap.Canvas.FillRect(Rect(0, 0, FTempBitmap.Width, FTempBitmap.Height));
          FTempBitmap.Canvas.StretchDraw(Rect(0, 0, FTempBitmap.Width, FTempBitmap.Height), Picture.Graphic);
          Canvas.Draw(R.Left, R.Top, FTempBitmap);
        except
          on EOutOfResources do
          begin
            Canvas.Brush.Color := BackgroundColor;
            Canvas.FillRect(ClientRect);
            Canvas.StretchDraw(R, Picture.Graphic);
          end;
        end;
      end
      else
        Canvas.StretchDraw(R, Picture.Graphic);
      NeedDrawBkg := not (IntersectRect(Temp, R, CR) and EqualRect(Temp, CR)) and not Self.Transparent;
      if NeedDrawBkg then
      begin
        ExcludeClipRect(R);
        Brush.Color := BackgroundColor;
        FillRect(CR);
      end;
      SetClipRegion(SaveRgn, roSet);
    end;
  except
    ACanvas.Brush.Color := clWhite;
    ACanvas.Canvas.FillRect(ClientRect);
    ACanvas.DrawTexT('无法查看此图片,因为此文件可能已损坏、损毁或者过大', ClientRect,   DT_VCENTER or DT_CENTER);
  end;


try
....................
  except
    ACanvas.Brush.Color := clWhite;
    ACanvas.Canvas.FillRect(ClientRect);
    ACanvas.DrawTexT('无法查看此图片,因为此文件可能已损坏、损毁或者过大', ClientRect,   DT_VCENTER or DT_CENTER);
  end;

此处是我新增部分,其余是原有的,你们还有什么好的处理方式吗?

  • 打赏
  • 点赞
  • 收藏
  • 分享
共有 人打赏支持
通吃岛-低手哥
粉丝 16
博文 41
码字总数 9808
×
通吃岛-低手哥
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: