VB2005で画像ファイルの特定の色を補正して保存するには


min  2007-03-23 05:16:33  No: 98367

既存のピクチャーイメージ(黒い文字だけ)に対して、VB6で言うところの”Drawmode=Merge Not Pen”
で上書きすることによって、青い線は白く、黒い線上をなぞると黄色に変化させて保存をするという操作を実現したいのですが、
APIを使うとMerge Not Penの機能で描画に成功しますが、保存すると
修正を加えたところが全く反映されません。下のソースで

Dim g As Graphics = PictureBox1.CreateGraphics
としているところが間違いというのはわかるのですが、

Dim g As Graphics = Graphics.FromImage(PictureBox1.Image)
とすると、Merge Not Penの機能がおかしくなり、軌跡が全て黄色になるので困っています。
何か良い方法はありませんでしょうか。お願いいたします。

'WinGDI.hの定義
Const PS_SOLID As Integer = 0
Const R2_COPYPEN As Integer = 13 '通常用
Const R2_MERGENOTPEN As Integer = 12 '軌跡付消去用

Dim g As Graphics
Dim m_delPen As Integer
Dim m_Pen As Integer
Dim m_StartPoint As Point

'API関数プロトタイプ宣言
Declare Auto Function SetROP2 Lib "gdi32.dll" Alias "SetROP2" (ByVal hdc As IntPtr, ByVal fnDrawMode As Integer) As Integer
Declare Auto Function GetROP2 Lib "gdi32.dll" Alias "GetROP2" (ByVal hdc As IntPtr) As Integer
Declare Auto Function CreatePen Lib "gdi32.dll" Alias "CreatePen" (ByVal fnPenStyle As Integer, ByVal nWidth As Integer, ByVal crColor As Integer) As IntPtr
Declare Auto Function SelectObject Lib "gdi32.dll" Alias "SelectObject" (ByVal hdc As IntPtr, ByVal hObject As IntPtr) As IntPtr
Declare Auto Function MoveToEx Lib "gdi32.dll" Alias "MoveToEx" (ByVal hdc As IntPtr, ByVal X As Integer, ByVal Y As Integer, ByVal lpPoint As IntPtr) As Boolean
Declare Auto Function LineTo Lib "gdi32.dll" Alias "LineTo" (ByVal hdc As IntPtr, ByVal X As Integer, ByVal Y As Integer) As Boolean
Private Declare Function GetDC Lib "user32" Alias "GetDC" (ByVal hWnd As Integer) As Integer

Private Sub Form_Load(
  m_Pen = CreatePen(PS_SOLID, 3, makeRGB(0, 0, 255))     '確定線の色(青)
  m_delPen = CreatePen(PS_SOLID, 7, makeRGB(0, 0, 255)) '消去線の色(青の補色)
  'PictureBox1.Image には既存bmpファイルをロード済み
  
Private Sub PictureBox1_MouseDown(
  DrawNow = True '開始
  Dim pictPoint As New Point(e.X, e.Y)  'マウスボタンが押下された座標
  m_StartPoint = pictPoint              '描画開始位置を記憶

Private Sub PictureBox1_MouseMove(
   Dim button As Short = e.Button \ &H100000

   Dim g As Graphics = PictureBox1.CreateGraphics

   Dim hdc As IntPtr = g.GetHdc()        '現在の描画モードを取得保存する
   updflg = True
   If button = 1 Then '左ボタン
       SetROP2(hdc, R2_COPYPEN)
       SelectObject(hdc, m_Pen)
   ElseIf button = 2 Then '右ボタン
       SetROP2(hdc, R2_MERGENOTPEN)
       SelectObject(hdc, m_delPen)
   End If
   '線を描画
   MoveToEx(hdc, m_StartPoint.X, m_StartPoint.Y, Nothing)
   LineTo(hdc, e.X, e.Y)
   g.ReleaseHdc(hdc)
   g.Dispose()

   '新しい点を記憶する
   m_StartPoint = pictPoint
End Sub

保存処理
PictureBox1.Image.Save(path & ".bmp")


※返信する前に利用規約をご確認ください。

※Google reCAPTCHA認証からCloudflare Turnstile認証へ変更しました。






  このエントリーをはてなブックマークに追加