Click here to Skip to main content
15,894,955 members
Home / Discussions / C#
   

C#

 
QuestionTransform a file from Word to PDF and from PDF to Word Pin
abbd11-Feb-09 5:10
abbd11-Feb-09 5:10 
AnswerRe: Transform a file from Word to PDF and from PDF to Word Pin
Giorgi Dalakishvili11-Feb-09 6:17
mentorGiorgi Dalakishvili11-Feb-09 6:17 
GeneralRe: Transform a file from Word to PDF and from PDF to Word Pin
DJ-Boris11-Feb-09 6:50
DJ-Boris11-Feb-09 6:50 
GeneralRe: Transform a file from Word to PDF and from PDF to Word Pin
abbd11-Feb-09 21:11
abbd11-Feb-09 21:11 
Questionretaining alpha channels from 32bpp bmp Pin
Doof36911-Feb-09 5:01
Doof36911-Feb-09 5:01 
AnswerRe: retaining alpha channels from 32bpp bmp Pin
Dave Kreskowiak11-Feb-09 6:42
mveDave Kreskowiak11-Feb-09 6:42 
AnswerRe: retaining alpha channels from 32bpp bmp Pin
Guffa11-Feb-09 9:55
Guffa11-Feb-09 9:55 
AnswerRe: retaining alpha channels from 32bpp bmp Pin
Doof36912-Feb-09 4:32
Doof36912-Feb-09 4:32 
Thanks for the replies.
ok. So this is what I have now, which actually works Smile | :) ))



public static void ArgbOverlay(Bitmap bm2, Bitmap bm1, out Bitmap bm3)
        {
            //Create Manipulation Arrays
            int[,] a1 = new int[bm1.Width, bm1.Height];
            int[,] a2 = new int[bm2.Width, bm2.Height];
            int[,] a3 = new int[bm2.Width, bm2.Height];

            int[,] r1 = new int[bm1.Width, bm1.Height];
            int[,] r2 = new int[bm2.Width, bm2.Height];
            int[,] r3 = new int[bm2.Width, bm2.Height];

            int[,] g1 = new int[bm1.Width, bm1.Height];
            int[,] g2 = new int[bm2.Width, bm2.Height];
            int[,] g3 = new int[bm2.Width, bm2.Height];

            int[,] b1 = new int[bm1.Width, bm1.Height];
            int[,] b2 = new int[bm2.Width, bm2.Height];
            int[,] b3 = new int[bm2.Width, bm2.Height];


            //Copy Images into arrays
            Rectangle rect1 = new Rectangle(0, 0, bm1.Width, bm1.Height);
            BitmapData bd1 = bm1.LockBits(rect1, ImageLockMode.ReadWrite, bm1.PixelFormat);
            int bytes1 = bd1.Stride * bd1.Height;
            byte[] argbValues1 = new byte[bytes1];
            IntPtr ptr1 = bd1.Scan0;
            Marshal.Copy(ptr1, argbValues1, 0, bytes1);

            Rectangle rect2 = new Rectangle(0, 0, bm2.Width, bm2.Height);
            BitmapData bd2 = bm2.LockBits(rect2, ImageLockMode.ReadWrite, bm2.PixelFormat);
            int bytes2 = bd2.Stride * bd2.Height;
            byte[] argbValues2 = new byte[bytes2];
            IntPtr ptr2 = bd2.Scan0;
            Marshal.Copy(ptr2, argbValues2, 0, bytes2);

            //Create Array for result
            // Create the bitmap
            bm3 = new Bitmap(bm1.Width, bm1.Height, PixelFormat.Format32bppArgb);
            // Create a byte array to match the bitmap
            Rectangle rect3 = new Rectangle(0, 0, bm3.Width, bm3.Height);
            BitmapData bd3 = bm3.LockBits(rect3, ImageLockMode.ReadWrite, bm3.PixelFormat);
            int bytes3 = bd3.Stride * bd3.Height;  // not bm.Width * bm.Height * 3;  
            byte[] argbValues3 = new byte[bytes3];


            //Separate a, r, g & b components
            for (int x = 0; x < bm1.Width; x++)
            {
                for (int y = 0; y < bm1.Height; y++)
                {
                    int BitPos1 = (x * 4) + (y * bd1.Stride);
                    b1[x, y] = (int)argbValues1[BitPos1];
                    g1[x, y] = (int)argbValues1[BitPos1 + 1];
                    r1[x, y] = (int)argbValues1[BitPos1 + 2];
                    a1[x, y] = (int)argbValues1[BitPos1 + 3];

                    b2[x, y] = (int)argbValues2[BitPos1];
                    g2[x, y] = (int)argbValues2[BitPos1 + 1];
                    r2[x, y] = (int)argbValues2[BitPos1 + 2];
                    a2[x, y] = (int)argbValues2[BitPos1 + 3];


                    //Test Transform (Overlay 2nd bitmap over the 1st using alpha layer)
                    r3[x, y] = (r2[x, y] * a2[x, y]) / 256 + ((r1[x, y] * (256 - a2[x, y])) / 256);
                    g3[x, y] = (g2[x, y] * a2[x, y]) / 256 + ((g1[x, y] * (256 - a2[x, y])) / 256);
                    b3[x, y] = (b2[x, y] * a2[x, y]) / 256 + ((b1[x, y] * (256 - a2[x, y])) / 256);

                    if (a1[x, y] < a2[x, y])
                    {
                        a3[x, y] = a2[x, y];
                    }
                    else
                    {
                        a3[x, y] = a1[x, y];
                    }


                    argbValues3[BitPos1] = (byte)b3[x, y];
                    argbValues3[BitPos1 + 1] = (byte)g3[x, y];
                    argbValues3[BitPos1 + 2] = (byte)r3[x, y];
                    argbValues3[BitPos1 + 3] = (byte)a3[x, y];
                }
            }


            // Copy the resulting byte array into the bitmap and unlock it
            IntPtr ptr3 = bd3.Scan0;
            Marshal.Copy(argbValues3, 0, ptr3, bytes3);
            bm3.UnlockBits(bd3);

            return;
        }


Was wondering if anyone can see any glaring ways to optimise this further to make it a less cumbersome piece of code and also to make it run faster if poss.
It is fine as it is, but less is more.
QuestionSend data from textbox to datagridview in a diferent form Pin
ZRF6911-Feb-09 3:58
ZRF6911-Feb-09 3:58 
AnswerRe: Send data from textbox to datagridview in a diferent form Pin
ByteBlocks11-Feb-09 4:09
ByteBlocks11-Feb-09 4:09 
AnswerRe: Send data from textbox to datagridview in a diferent form Pin
musefan11-Feb-09 4:16
musefan11-Feb-09 4:16 
AnswerRe: Send data from textbox to datagridview in a diferent form [modified] Pin
DaveyM6911-Feb-09 9:48
professionalDaveyM6911-Feb-09 9:48 
AnswerRe: Send data from textbox to datagridview in a diferent form Pin
Deresen11-Feb-09 10:12
Deresen11-Feb-09 10:12 
QuestionOverride desktop context menu [modified] Pin
pmcons11-Feb-09 3:55
pmcons11-Feb-09 3:55 
AnswerRe: Override desktop context menu Pin
jas0n2311-Feb-09 4:02
jas0n2311-Feb-09 4:02 
GeneralRe: Override desktop context menu Pin
pmcons11-Feb-09 4:06
pmcons11-Feb-09 4:06 
AnswerRe: Override desktop context menu Pin
Giorgi Dalakishvili11-Feb-09 4:56
mentorGiorgi Dalakishvili11-Feb-09 4:56 
GeneralRe: Override desktop context menu Pin
pmcons11-Feb-09 5:31
pmcons11-Feb-09 5:31 
QuestionA New Language In C# Pin
jas0n2311-Feb-09 3:50
jas0n2311-Feb-09 3:50 
AnswerRe: A New Language In C# Pin
musefan11-Feb-09 4:11
musefan11-Feb-09 4:11 
AnswerRe: A New Language In C# Pin
Pete O'Hanlon11-Feb-09 4:29
mvePete O'Hanlon11-Feb-09 4:29 
AnswerRe: A New Language In C# Pin
riced11-Feb-09 4:46
riced11-Feb-09 4:46 
GeneralRe: A New Language In C# Pin
PIEBALDconsult11-Feb-09 4:59
mvePIEBALDconsult11-Feb-09 4:59 
AnswerRe: A New Language In C# Pin
PIEBALDconsult11-Feb-09 5:05
mvePIEBALDconsult11-Feb-09 5:05 
GeneralRe: A New Language In C# Pin
jas0n2323-Apr-09 5:04
jas0n2323-Apr-09 5:04 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.