Welcome Guest! You need to login or register to make posts.

Notification

Icon
Error

2 Pages12>
Options
Go to last post Go to first unread
Fedor  
#1 Posted : Wednesday, December 03, 2003 9:08:00 PM(UTC)
Fedor

Rank: Advanced Member

Groups: Member, Administration, Moderator
Joined: 7/28/2003(UTC)
Posts: 1,547

Thanks: 5 times
Was thanked: 59 time(s) in 58 post(s)
Hello everyone, I have made a code sample which produces Photoshop-like crop mask using DoubleBufferPaint event (see the screenshot) Screenshot: UserPostedImage Use this code:
Code:
Private Sub CommandLoad_Click()
    If FileDialog1.ShowOpen Then
        On Error Resume Next
        BitmapViewer1.Bitmap.LoadFromFile FileDialog1.FileName
        If Err.Number <> 0 Then
            MsgBox "Error arrised during loading image."
        End If
    End If
End Sub

Private Sub BitmapViewer1_AfterSelect(ByVal Left As Long, ByVal Top As Long, ByVal Width As Long, ByVal Height As Long)
    If BitmapViewer1.SelectionWidth > 0 And BitmapViewer1.SelectionHeight Then
        CommandCrop.Enabled = True
    Else
        CommandCrop.Enabled = False
    End If
End Sub

Private Sub BitmapViewer1_DoubleBufferPaint(ByVal Hdc As stdole.OLE_HANDLE)
    If BitmapViewer1.SelectionWidth > 0 And BitmapViewer1.SelectionHeight > 0 Then
        Dim objGraphics As New Graphics
        'Attach graphics object to temporary device context
        'which is used for double buffer painting
        objGraphics.Hdc = Hdc
        objGraphics.Engine = DrawingEngineGdiplus
        objGraphics.Antialiasing = False
        objGraphics.Brush.PrimaryColor = &H80FF0000
        Dim X1 As Long, Y1 As Long, X2 As Long, Y2 As Long

        'To shade a part of image which should be cropped,
        'we will draw four semi-transparent rectangles.

        'Draw top part
        'Convert bitmap coordinates to control and draw a top rectangle
        BitmapViewer1.BitmapToControlCoords 0, 0, X1, Y1
        BitmapViewer1.BitmapToControlCoords BitmapViewer1.Bitmap.Data.Width, _
            BitmapViewer1.SelectionTop, X2, Y2
        objGraphics.DrawRectangle X1, Y1, X2 - X1, Y2 - Y1, True, False
        'Draw bottom part
        'Convert bitmap coordinates to control and draw a bottom rectangle
        BitmapViewer1.BitmapToControlCoords 0, _
            BitmapViewer1.SelectionTop + BitmapViewer1.SelectionHeight, X1, Y1
        BitmapViewer1.BitmapToControlCoords BitmapViewer1.Bitmap.Data.Width, _
            BitmapViewer1.Bitmap.Data.Height, X2, Y2
        objGraphics.DrawRectangle X1, Y1, X2 - X1, Y2 - Y1, True, False
        'Draw left part
        'Convert bitmap coordinates to control and draw a left rectangle
        BitmapViewer1.BitmapToControlCoords 0, BitmapViewer1.SelectionTop, X1, Y1
        BitmapViewer1.BitmapToControlCoords BitmapViewer1.SelectionLeft, _
            BitmapViewer1.SelectionTop + BitmapViewer1.SelectionHeight, X2, Y2
        objGraphics.DrawRectangle X1, Y1, X2 - X1, Y2 - Y1, True, False
        'Draw right part
        'Convert bitmap coordinates to control and draw a right rectangle
        BitmapViewer1.BitmapToControlCoords BitmapViewer1.SelectionLeft + BitmapViewer1.SelectionWidth, _
            BitmapViewer1.SelectionTop, X1, Y1
        BitmapViewer1.BitmapToControlCoords BitmapViewer1.Bitmap.Data.Width, _
            BitmapViewer1.SelectionTop + BitmapViewer1.SelectionHeight, X2, Y2
        objGraphics.DrawRectangle X1, Y1, X2 - X1, Y2 - Y1, True, False
    End If
End Sub
I have attached full project code.

Edited by user Monday, December 21, 2009 3:15:21 AM(UTC)  | Reason: Not specified

File Attachment(s):
GraphicsMill_CropMask.zip (10kb) downloaded 172 time(s).
Fedor attached the following image(s):
GraphicsMill_CropMask.JPG
Best regards, Fedor Skvortsov
jvv  
#2 Posted : Thursday, October 14, 2004 6:08:00 PM(UTC)
jvv

Rank: Member

Groups: Member
Joined: 10/14/2004(UTC)
Posts: 37

I want to use the crop mask in Delphi. I copied your VB code exactly to my delphi application. I translated it to Pascal and run my source. When I try to make a selection, it generates an error: [Image was removed] I mostly use TIFF images. Thanks in advance...

Edited by user Friday, December 18, 2009 8:37:31 PM(UTC)  | Reason: Not specified

Andrew  
#3 Posted : Thursday, October 14, 2004 9:17:00 PM(UTC)
Andrew

Rank: Advanced Member

Groups: Member, Administration
Joined: 8/2/2003(UTC)
Posts: 866

Thanks: 2 times
Was thanked: 26 time(s) in 26 post(s)
As far as I understand you are trying to use crop mask at 1-bit TIFF images (or possible 4 or 8-bit ones). Alpha blending is not possible at such images. Try to use plain GDI with hatched brush instead of semi-transparent GDI+ brush. For this modify DoubleBufferPaint event handler, in particular initialization of objGraphics (I will write at VB6, I think it won't make any problem to convert it into Pascal):
Code:
'Attach graphics object to temporary device context
'which is used for double buffer painting

objGraphics.Hdc = Hdc
objGraphics.Engine = DrawingEngineGdi
objGraphics.Antialiasing = False

objGraphics.Brush.Type = BrushTypeHatch
objGraphics.Brush.PrimaryColor = ColorBlack
objGraphics.Brush.IsBackgroundTransparent = True
objGraphics.Brush.HatchStyle = HatchStyleForwardDiagonal
Hope this helps.

Edited by user Thursday, December 20, 2007 6:41:30 PM(UTC)  | Reason: Not specified

jvv  
#4 Posted : Thursday, October 14, 2004 9:26:00 PM(UTC)
jvv

Rank: Member

Groups: Member
Joined: 10/14/2004(UTC)
Posts: 37

It doesn't work... The error is now: 'Cannot use Graphics with indexed bitmaps'. Thanx anyway...
Andrew  
#5 Posted : Thursday, October 14, 2004 9:55:00 PM(UTC)
Andrew

Rank: Advanced Member

Groups: Member, Administration
Joined: 8/2/2003(UTC)
Posts: 866

Thanks: 2 times
Was thanked: 26 time(s) in 26 post(s)
I just noticed that you trying to draw directly at bitmap (). It is not correct. You should create separate Graphics object and assign HDC which is retrieved as an argument of the DoubleBufferPaint event. Instead of
Code:
var X1, Y1, X2, Y2: Integer;
begin
  if (TiffViewer.SelectionWidth > 0) and (TiffViewer.SelectionHeight > 0) then
  begin
      //'Attach graphics object to temporary device context
      //'which is used for double buffer painting

      TiffViewer.Bitmap.Graphics.Engine := DrawingEngineGdiplus;
      TiffViewer.Bitmap.Graphics.Antialiasing := False;
      TiffViewer.Bitmap.Graphics.Brush.PrimaryColor := $80FF0000;
Use
Code:
var X1, Y1, X2, Y2: Integer;
      objGraphics: TGraphics;
begin
  if (TiffViewer.SelectionWidth > 0) and (TiffViewer.SelectionHeight > 0) then
  begin
      //'Attach graphics object to temporary device context
      //'which is used for double buffer painting
      
      objGraphics := TGraphics.Create();

      objGraphics.Engine := DrawingEngineGdiplus;
      objGraphics.Antialiasing := False;
      objGraphics.Brush.PrimaryColor := $80FF0000;
BTW, it will likely work with GDI+ (because you will draw not at indexed bitmap, but at HDC which contains 24-bit double buffer). Please let me know if it helps. Note, I used to program with Delphi a long time ago, so I may make a mistake in syntax somewhere, but I think you will get an idea.

Edited by user Thursday, December 20, 2007 6:41:50 PM(UTC)  | Reason: Not specified

jvv  
#6 Posted : Thursday, October 14, 2004 10:16:00 PM(UTC)
jvv

Rank: Member

Groups: Member
Joined: 10/14/2004(UTC)
Posts: 37

It doesn't work too :( The object TGraphics doesn't exist. I also tried the TGraphicObeject, TGraphic and TCanvas, the all don't seem to work. I also experienced another problem. When I try to save a TIFF image, after cropping, it is saved with JPEG compression, and I can't find any solution to turn the JPEG compression off... Thanks anyway for your time...
Andrew  
#7 Posted : Thursday, October 14, 2004 10:27:00 PM(UTC)
Andrew

Rank: Advanced Member

Groups: Member, Administration
Joined: 8/2/2003(UTC)
Posts: 866

Thanks: 2 times
Was thanked: 26 time(s) in 26 post(s)
> The object TGraphics doesn't exist. I also tried the TGraphicObeject, TGraphic and TCanvas, the all don't seem to work. I am not sure what name it is imported with. The coclass is called Graphics, but maybe TGraphics already exists in standard Delphi classes and it has renamed it. Try to find file GraphicsMill20_TLB.pas (or something like that). It should contain all definitions imported from type library into Delphi component. Try to find how this class is called here. Besides, make sure that your module contains GraphicsMill20_TLB in uses statement. > I also experienced another problem. When I try to save a TIFF image, after cropping, it is saved with JPEG compression, and I can't find any solution to turn the JPEG compression off... Please post a code snippet you try to save TIFF file with.
jvv  
#8 Posted : Thursday, October 14, 2004 11:16:00 PM(UTC)
jvv

Rank: Member

Groups: Member
Joined: 10/14/2004(UTC)
Posts: 37

Ofcourse I included the GraphicsMill unit in my uses :) I'll try to use the IEmbeddedGraphics object in my program. This is the code snippet when I try try to save the file:
Code:
    TiffViewer.Bitmap.LoadFromFile(Query.FieldByName('BRON').AsString);
    TiffViewer.Bitmap.Transforms.Crop(X, Y, RectWidth, RectHeight, $FFFFFFFF);
    TiffViewer.Bitmap.Formats.TiffCompressionType := 5;
    TiffViewer.Bitmap.SaveToFile(Query.FieldByName('DOEL').AsString);

Edited by user Thursday, December 20, 2007 6:42:11 PM(UTC)  | Reason: Not specified

Andrew  
#9 Posted : Friday, October 15, 2004 1:51:00 PM(UTC)
Andrew

Rank: Advanced Member

Groups: Member, Administration
Joined: 8/2/2003(UTC)
Posts: 866

Thanks: 2 times
Was thanked: 26 time(s) in 26 post(s)
> Ofcourse I included the GraphicsMill unit in my uses :) > I'll try to use the IEmbeddedGraphics object in my program. You can declare IGraphics in var, but to create the object, you should use coclass. I found out that coclass Graphics was generated with name CoGraphics. > This is the code snippet when I try try to save the file: > [...] Try this one:
Code:


TiffViewer.Bitmap.LoadFromFile(Query.FieldByName('BRON').AsString);
TiffViewer.Bitmap.Transforms.Crop(X, Y, RectWidth, RectHeight, $FFFFFFFF);

TiffViewer.Bitmap.FormatAutoSelect := False;
TiffViewer.Bitmap.Formats.SelectCurrent("TIFF");

TiffViewer.Bitmap.Formats.TiffCompressionType := 5;
TiffViewer.Bitmap.SaveToFile(Query.FieldByName('DOEL').AsString);
Please keep me informed if it helps.
jvv  
#10 Posted : Monday, October 18, 2004 3:51:00 PM(UTC)
jvv

Rank: Member

Groups: Member
Joined: 10/14/2004(UTC)
Posts: 37

I discovered the problem with the wrong compression... The headers in GraphicsMill are not written completely, like in Imaging Professional. A few important header tags are not written at all. The cropmask isn't working yet. Still the same errors... :(
Andrew  
#11 Posted : Monday, October 18, 2004 6:43:00 PM(UTC)
Andrew

Rank: Advanced Member

Groups: Member, Administration
Joined: 8/2/2003(UTC)
Posts: 866

Thanks: 2 times
Was thanked: 26 time(s) in 26 post(s)
> I discovered the problem with the wrong compression... Could you tell more detailed what is wrong with the compression? Did you try the code above (assign FormatAutoSelect to False). > The headers in GraphicsMill are not written completely, like in Imaging Professional. > A few important header tags are not written at all. Graphics Mill 2.0 does not handle metainformation in TIFF. Version 3.0 will allow working with fields covered by EXIF though. > The cropmask isn't working yet. > Still the same errors... :( Error occurs only when you try to draw directly at bitmap (which is not correctly) instead of HDC of the bitmap viewer window. Did you manage to create Graphics object?
jvv  
#12 Posted : Monday, October 18, 2004 6:58:00 PM(UTC)
jvv

Rank: Member

Groups: Member
Joined: 10/14/2004(UTC)
Posts: 37

The compression itself isn't wrong... The problem is in the TIFF headers. I already submitted the problem with the "Response Problem" option on the site. I'm trying to work with the HDC soon, if I get time for it. The feature isn't not very important for use in our application, it's only for a nicer looking selection...
Andrew  
#13 Posted : Monday, October 18, 2004 7:04:00 PM(UTC)
Andrew

Rank: Advanced Member

Groups: Member, Administration
Joined: 8/2/2003(UTC)
Posts: 866

Thanks: 2 times
Was thanked: 26 time(s) in 26 post(s)
> The compression itself isn't wrong... > The problem is in the TIFF headers. > I already submitted the problem with the "Response Problem" option on the site. Ok, let me check. > I'm trying to work with the HDC soon, if I get time for it. The feature isn't not very important > for use in our application, it's only for a nicer looking selection... I just tried to create test application at Delphi with the following DoubleBufferPaint event handler, and it worked perfectly:
Code:
procedure TForm1.TiffViewerDoubleBufferPaint(ASender: TObject;
  Hdc: Cardinal);
var X1, Y1, X2, Y2: Integer;
    objGraphics: IGraphics;
begin
  if (TiffViewer.SelectionWidth > 0) and (TiffViewer.SelectionHeight > 0) then
  begin

      objGraphics := CoGraphics.Create;

      //'Attach graphics object to temporary device context
      //'which is used for double buffer painting

      objGraphics.hdc := Hdc;
      objGraphics.Engine := DrawingEngineGdiplus;
      objGraphics.Antialiasing := False;

      objGraphics.Brush.PrimaryColor := $80FF0000;
    //'To shade a part of image which should be cropped,
    //  'we will draw four semi-transparent rectangles.

    //'Draw top part
    //  'Convert bitmap coordinates to control and draw a top rectangle
      TiffViewer.BitmapToControlCoords(0, 0, X1, Y1);
      TiffViewer.BitmapToControlCoords(TiffViewer.Bitmap.Data.Width, TiffViewer.SelectionTop, X2, Y2);
      objGraphics.DrawRectangle(X1, Y1, X2 - X1, Y2 - Y1, True, False, 0);

    //  'Draw bottom part
    // 'Convert bitmap coordinates to control and draw a bottom rectangle
      TiffViewer.BitmapToControlCoords(0, TiffViewer.SelectionTop + TiffViewer.SelectionHeight, X1, Y1);
      TiffViewer.BitmapToControlCoords(TiffViewer.Bitmap.Data.Width, TiffViewer.Bitmap.Data.Height, X2, Y2);
      objGraphics.DrawRectangle(X1, Y1, X2 - X1, Y2 - Y1, True, False, 0);


    //  'Draw left part
    //  'Convert bitmap coordinates to control and draw a left rectangle
      TiffViewer.BitmapToControlCoords(0, TiffViewer.SelectionTop, X1, Y1);
      TiffViewer.BitmapToControlCoords(TiffViewer.SelectionLeft, TiffViewer.SelectionTop + TiffViewer.SelectionHeight, X2, Y2);
      objGraphics.DrawRectangle(X1, Y1, X2 - X1, Y2 - Y1, True, False, 0);
    //  'Draw right part
    //  'Convert bitmap coordinates to control and draw a right rectangle
      TiffViewer.BitmapToControlCoords(TiffViewer.SelectionLeft + TiffViewer.SelectionWidth, TiffViewer.SelectionTop, X1, Y1);
      TiffViewer.BitmapToControlCoords(TiffViewer.Bitmap.Data.Width, TiffViewer.SelectionTop + TiffViewer.SelectionHeight, X2, Y2);
      objGraphics.DrawRectangle(X1, Y1, X2 - X1, Y2 - Y1, True, False, 0);
  end;
end;
Hope this helps.

Edited by user Thursday, December 20, 2007 6:42:29 PM(UTC)  | Reason: Not specified

Andrew  
#14 Posted : Monday, October 18, 2004 7:13:00 PM(UTC)
Andrew

Rank: Advanced Member

Groups: Member, Administration
Joined: 8/2/2003(UTC)
Posts: 866

Thanks: 2 times
Was thanked: 26 time(s) in 26 post(s)
Please check the mailbox.
jvv  
#15 Posted : Monday, October 18, 2004 8:23:00 PM(UTC)
jvv

Rank: Member

Groups: Member
Joined: 10/14/2004(UTC)
Posts: 37

Thanks for all of your support... The CropMask is working fine :) I think I forgot a simple thing... I'm going to check for the compression and header information as soon as possible... Thanks again...
jvv  
#16 Posted : Monday, October 18, 2004 8:39:00 PM(UTC)
jvv

Rank: Member

Groups: Member
Joined: 10/14/2004(UTC)
Posts: 37

The problem with the headers is not solved with the new DLL file. Here are 2 screenshots with the header information: Before cropping: [Image was removed] After cropping: [Image was removed]

Edited by user Saturday, December 19, 2009 7:02:28 PM(UTC)  | Reason: Not specified

Andrew  
#17 Posted : Monday, October 18, 2004 9:31:00 PM(UTC)
Andrew

Rank: Advanced Member

Groups: Member, Administration
Joined: 8/2/2003(UTC)
Posts: 866

Thanks: 2 times
Was thanked: 26 time(s) in 26 post(s)
You are right, it seems we have fixed it only in Graphics Mill 3.0. I will send you an update shortly.
jvv  
#18 Posted : Monday, October 18, 2004 9:52:00 PM(UTC)
jvv

Rank: Member

Groups: Member
Joined: 10/14/2004(UTC)
Posts: 37

Thank you for your great support :) Do you already have a planned release date for version 3?
Andrew  
#19 Posted : Monday, October 18, 2004 10:42:00 PM(UTC)
Andrew

Rank: Advanced Member

Groups: Member, Administration
Joined: 8/2/2003(UTC)
Posts: 866

Thanks: 2 times
Was thanked: 26 time(s) in 26 post(s)
We plan to release stable beta in the first half of November. If no critical die-hard bugs will be found, release will be available in a couple weeks since beta release. However Graphics Mill 3.0 will be a .NET version only. We will probably release ActiveX version later, but the our main focus will be at .NET market.
jvv  
#20 Posted : Thursday, October 21, 2004 3:11:00 PM(UTC)
jvv

Rank: Member

Groups: Member
Joined: 10/14/2004(UTC)
Posts: 37

Andrew, the dll I received from you is working correct... Thanks... I'm looking forward to version 3 :) (ActiveX ;))
Users browsing this topic
2 Pages12>
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.