skit091 0 Newbie Poster

Main form coding ;-

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace CS3DGeneratorSample
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

    private void link_Image_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
    {
        OpenFileDialog.Title = "Open Image";
        if (OpenFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            pn_image.BackgroundImage = Image.FromFile(OpenFileDialog.FileName);
        }
    }

    private void link_Depth_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
    {
        OpenFileDialog.Title = "Open DepthMap";
        if (OpenFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            pn_depth.BackgroundImage = Image.FromFile(OpenFileDialog.FileName);
        }
    }


    private void but_anag_Click(object sender, EventArgs e)
    {
        _3DImageGenerator.c_3DGenerator gen = Init3DGenerator();
        if (gen == null)
            return;

        pb_loading.Visible = true;
        gen.GenerateAnaglyphAsync((Bitmap)pn_image.BackgroundImage, (Bitmap)pn_depth.BackgroundImage, gen);
    }

    private void AnaglypthFinished(bool success, object usercode)
    {
        if (success == false)
        {
            MessageBox.Show("Failed!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        else
        {
            pn_output.BackgroundImage = ((_3DImageGenerator.c_3DGenerator)usercode).Anaglyph;
            link_output.Tag = usercode;
        }
        pb_loading.Visible = false;
    }



    private _3DImageGenerator.c_3DGenerator Init3DGenerator()
    {
        if (pn_image.BackgroundImage == null || pn_depth.BackgroundImage == null)
        {
            MessageBox.Show("You must select Image and Depthmap before generating.","Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
            return null;
        }
        _3DImageGenerator.c_3DGenerator gen = new _3DImageGenerator.c_3DGenerator();
       // gen.Smoothing = cb_smoothing.Checked;
        gen.MaxPixelDisplacement = (int)nud_maxdisp.Value;
       // gen.SwapRightLeft = cb_swap.Checked;
       // gen.InverseDepthMap = cb_inverse.Checked;
        gen.AnaglyphComplete += AnaglypthFinished;
       // gen.StereoscopicComplete += StereoscopicFinished;
        return gen;
    }

    private void link_output_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
    {
        if (((LinkLabel)sender).Tag != null)
        {
            _3DImageGenerator.c_3DGenerator gen = ((_3DImageGenerator.c_3DGenerator)((LinkLabel)sender).Tag);
            if (gen.Anaglyph != null)
            {
                SaveFileDialog.Title = "Save Anaglyph";
                SaveFileDialog.FileName = "";
                SaveFileDialog.Filter = "Jpg File|*.jpg|BMP File|*.bmp|PNG File|*.png";
                if (SaveFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {

                    switch (System.IO.Path.GetExtension(SaveFileDialog.FileName).ToLower().Trim())
                    {
                        case "png":
                            gen.SaveAnaglyph(SaveFileDialog.FileName, System.Drawing.Imaging.ImageFormat.Png);
                            break;
                        case "bmp":
                            gen.SaveAnaglyph(SaveFileDialog.FileName, System.Drawing.Imaging.ImageFormat.Bmp);
                            break;
                        default:
                            gen.SaveAnaglyph(SaveFileDialog.FileName, System.Drawing.Imaging.ImageFormat.Jpeg , 100);
                            break;
                    }
                }
            }
            else
            {

            }
        }

    }

    private void pb_loading_Click(object sender, EventArgs e)
    {

    }



}

}

this is another file in vb to connect

Imports System.Runtime.InteropServices.Marshal
Imports System.Drawing.Imaging
Imports System.Drawing

Public Class c_3DGenerator
Private i_maxDisplacement As Integer = 12

Private b_Anaglyph As Bitmap

Private b_SwapRL As Boolean = False

Public Event AnaglyphComplete(ByVal Success As Boolean, ByVal userCode As Object)

Public Sub New()
    '' Nothing to do
End Sub
Public Sub GenerateAnaglyphAsync(ByVal Image As Bitmap, ByVal DepthMap As Bitmap, ByVal userCode As Object)
    '' Creating a thread for doing work in background. It will raise AnaglyphComplete when opration ended
    Dim th As Threading.Thread = New Threading.Thread(Sub()
                                                          Dim success As Boolean = GenerateAnaglyph(Image, DepthMap)
                                                          For Each del As System.Delegate In AnaglyphCompleteEvent.GetInvocationList()
                                                              If (del.Target Is Nothing OrElse CType(del.Target, ComponentModel.ISynchronizeInvoke).InvokeRequired = False) Then
                                                                  del.DynamicInvoke(New Object() {success, userCode})
                                                              Else
                                                                  CType(del.Target, ComponentModel.ISynchronizeInvoke).Invoke(del, New Object() {success, userCode})
                                                              End If
                                                          Next
                                                      End Sub)
    '' Starting thread
    th.Start()
End Sub

Public Function GenerateAnaglyph(ByVal Image As Bitmap, ByVal DepthMap As Bitmap) As Boolean
    '' Checking if image and depthmap have same size
    If Image.Width <> DepthMap.Width OrElse Image.Height <> DepthMap.Height Then Throw New ArgumentException("Size of Image and DepthMap are not same.")
    '' Check if image and depthmap are 24bitRGB or not
    If Image.PixelFormat <> PixelFormat.Format24bppRgb OrElse Image.PixelFormat <> PixelFormat.Format24bppRgb Then Throw New ArgumentException("Image and/or DepthMap are/is not 24bitRGB")
    Try
        '' Locking image and depthmap so other threads cant access them when we work
        SyncLock Image : SyncLock DepthMap
                '' Create a clean bitmap for saving output
                b_Anaglyph = New Bitmap(DepthMap.Width, Image.Height)
                '' Create a rect object, Same size as bitmap. Need for direct access in memory
                Dim r_Anaglyph As Rectangle = New Rectangle(0, 0, DepthMap.Width, Image.Height)
                '' Calculating real width of image (By byte)
                Dim i_width As Integer = DepthMap.Width * 3
                If i_width Mod 4 <> 0 Then
                    i_width = 4 * (i_width / 4 + 1)
                End If

                '' How much we need to move each pixel per depth byte
                Dim hsrate As Double = i_maxDisplacement / 255

                '' Creating a rect object with same size. For Depth map
                Dim r_depth As Rectangle = New Rectangle(0, 0, DepthMap.Width, DepthMap.Height)
                '' Opening direct access to bitmap data in memory for Depth map
                Dim d_depth As BitmapData = DepthMap.LockBits(r_depth, ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format24bppRgb)

                '' Creating a rect object with same size. For Image
                Dim r_image As Rectangle = New Rectangle(0, 0, Image.Width, Image.Height)
                '' Opening direct access to bitmap data in memory for Image
                Dim d_image As BitmapData = Image.LockBits(r_image, ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format24bppRgb)

                '' Opening direct access to bitmap data in memory for Output Bitmap
                Dim d_anag As BitmapData = b_Anaglyph.LockBits(r_Anaglyph, ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Format24bppRgb)

                Dim sfp As Integer
                For y As Integer = 0 To DepthMap.Height - 1

                    '' Calculate location of current line's last free pixel
                    Dim rLPDest As IntPtr = (y + 1) * i_width - (i_maxDisplacement * 3) - 3

                    '' Calculate location of current line's first free pixel
                    Dim rFPDest As IntPtr = y * i_width + (i_maxDisplacement * 3)

                    '' Count for each pixel on width of image. Cut MaxDisplacementfrom both sides
                    For x As Integer = i_maxDisplacement To DepthMap.Width - 1 - i_maxDisplacement

                        '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Right 2 Left  <<--
                        '' Read Depth, Right to Left
                        Dim depthrgb As Byte = ReadByte(d_depth.Scan0 + rLPDest + 1)
                        If InverseDepthMap Then depthrgb = 255 - depthrgb

                        '' Calculate displacement offset, Right to Left
                        sfp = depthrgb * hsrate

                        '' Read BYTE from Image And Displace, Right to Left
                        If (Not b_SwapRL) Then
                            '' RED
                            WriteByte(d_anag.Scan0 + rLPDest + ((sfp) * 3) + 2, ReadByte(d_image.Scan0 + rLPDest + 2))
                        Else
                            '' BLUE
                            WriteByte(d_anag.Scan0 + rLPDest + ((sfp) * 3), ReadByte(d_image.Scan0 + rLPDest))
                            '' Read GREEM And Displace it just like BLUE
                            WriteByte(d_anag.Scan0 + rLPDest + ((sfp) * 3) + 1, ReadByte(d_image.Scan0 + rLPDest + 1))
                        End If



                    Next
                Next



                '' Closing direct access
                DepthMap.UnlockBits(d_depth)
                Image.UnlockBits(d_image)
                b_Anaglyph.UnlockBits(d_anag)
            End SyncLock : End SyncLock
        Return True
    Catch ex As Exception
        Return False
    End Try
End Function

Public ReadOnly Property Anaglyph As Bitmap
    Get
        Return b_Anaglyph
    End Get
End Property


Public Property MaxPixelDisplacement As Integer
    Get
        Return i_maxDisplacement
    End Get
    Set(ByVal value As Integer)
        i_maxDisplacement = value
    End Set
End Property

Public Property SwapRightLeft As Boolean
    Get
        Return b_SwapRL
    End Get
    Set(ByVal value As Boolean)
        b_SwapRL = value
    End Set
End Property

finally you can get 3d image

i m rohit gupta
my project was 2d to 3d image conversion
thanks..