Example #1
0
        public static Graphics FromImage(Image image)
        {
            if (image == null)
            {
                throw new ArgumentNullException("image");
            }

            if ((image.PixelFormat & PixelFormat.Indexed) != 0)
            {
                throw new Exception("Cannot create Graphics from an indexed bitmap.");
            }

            Bitmap b = image as Bitmap;

            if (b == null)
            {
                throw new Exception("Can not create Graphics contexts from " + image.GetType() + " Images, only Bitmaps are supported");
            }
            var cgimage = b.NativeCGImage;

            if (b.bitmapBlock == IntPtr.Zero)
            {
                throw new Exception("Missing functionality: currently we can not create graphics contexts from bitmaps loaded from disk, need to do some extra work");
            }

            // Creates a context using the parameters that were used initially for the bitmap,
            // reusing the memory address space on it as well.
            return(new Graphics(new CGBitmapContext(b.bitmapBlock, cgimage.Width, cgimage.Height, cgimage.BitsPerComponent, cgimage.BytesPerRow, cgimage.ColorSpace, cgimage.BitmapInfo)));
        }
Example #2
0
        public static string ConvertImageToBase64(System.Drawing.Image img)
        {
            var type   = img.GetType();
            var prefix = $"data:image/jpeg;base64,";

            using (MemoryStream m = new MemoryStream())
            {
                img.Save(m, img.RawFormat);
                byte[] imageBytes = m.ToArray();

                // Convert byte[] to Base64 String
                return(prefix + Convert.ToBase64String(imageBytes));
            }
        }
Example #3
0
        public static bool SaveImage(string fullpathname, sysImg.Imaging.ImageFormat imageFormat)
        {
            if (Clipboard.ContainsImage())
            {
                sysImg.Image image = Clipboard.GetImage();
                image.Save(fullpathname, sysImg.Imaging.ImageFormat.Png);
                return(true);
            }
            else if (Clipboard.ContainsData(DataFormats.MetafilePict))
            {
                sysImg.Image what = (sysImg.Image)Clipboard.GetData(DataFormats.MetafilePict);

                Debug.WriteLine(what.GetType());

                //image.Save(fullpathname, sysImg.Imaging.ImageFormat.Png);
                return(true);
            }
            else
            {
                Debug.WriteLine("NO IMAGE");
                return(false);
            }
        }
Example #4
0
    private void LoadPeople()
    {
        System.Drawing.Image image = System.Drawing.Image.FromFile(Server.MapPath("/Content/Images/Hexagon.png"));

        MemoryStream m = new MemoryStream();

        image.Save(m, image.RawFormat);
        byte[] imageArray = m.ToArray();

        using (SqlConnection con = new SqlConnection(Connection.ConnectionString))
        {
            con.Open();
            SqlCommand command = new SqlCommand("select Users_tbl.User_ID,FirstName,Surname,Gender,Friend_ID,Description,(select Firstname from Users_tbl where User_ID = Friend_ID) as 'FriendName',(select Surname from Users_tbl where User_ID = Friend_ID) as 'FriendSurname' from Users_tbl full outer join Friends_tbl" +
                                                " on Users_tbl.User_ID = Friends_tbl.User_ID full outer join Invitation_tbl on Friends_tbl.Invitation_ID = Invitation_tbl.Invite_ID", con);
            command.ExecuteNonQuery();
            SqlDataReader reader           = command.ExecuteReader();
            List <string> friends          = new List <string>();
            List <string> sentRequests     = new List <string>();
            List <string> receivedRequests = new List <string>();
            List <User>   registeredPeople = new List <User>();
            while (reader.Read())
            {
                try                                                                            //the method will try to read a null Friend_ID and throw an exception
                {
                    if (reader.GetInt32(4) == int.Parse(Session["LoggedInUserID"].ToString())) //if current logged user is a Friend to user in database
                    {
                        try
                        {
                            if (reader.GetString(5) == "Accepted")//Invite accepted
                            {
                                LoadFriends(reader.GetInt32(0).ToString(), reader.GetString(1), reader.GetString(2));
                                friends.Add(reader.GetInt32(0).ToString());
                            }//Invite accepted
                            else if (reader.GetString(5) == "Sent")//Invite sent
                            {
                                ulPending.InnerHtml += "<li class='list-group-item list-group-item-action' style='color: #7c2020'>" +
                                                       "<a href='#' >" +
                                                       "<img ID='NavPhoto" + reader.GetInt32(4) + "' class='border-light rounded rounded-circle text-center' src='data:image/" + image.GetType().ToString() + ";base64," + Convert.ToBase64String(imageArray) + "' style='background-color:white; Width:50px; Height:50px; margin-right:10px' />" +
                                                       reader.GetString(1) + " " + reader.GetString(2) +
                                                       "<span class='float-right fas  fa-check' style='color:green'></span></a>" +
                                                       "</li>";
                                sentRequests.Add(reader.GetInt32(0).ToString());
                            }//Invite sent
                             //else if (reader.GetString(5) == "Received")//Invite received
                             //{
                             //    LoadReceivedInvites(reader.GetInt32(0).ToString(), reader.GetString(1), reader.GetString(2));

                            //}//Invite received else
                        }
                        catch
                        {
                            try
                            {
                                if (reader.GetInt32(0) != int.Parse(Session["LoggedInUserID"].ToString()) &&
                                    !friends.Contains(reader.GetInt32(0).ToString()) &&
                                    !registeredPeople.Select(p => p.User_ID).Contains(reader.GetInt32(0)) &&
                                    !sentRequests.Contains(reader.GetInt32(0).ToString()) &&
                                    !receivedRequests.Contains(reader.GetInt32(0).ToString()))
                                {
                                    registeredPeople.Add(new User()
                                    {
                                        User_ID = reader.GetInt32(0), FirstName = reader.GetString(1), Surname = reader.GetString(2)
                                    });
                                }
                            }
                            catch
                            {
                            }
                        }
                    }
                    else if (reader.GetInt32(0) == int.Parse(Session["LoggedInUserID"].ToString()))                       //if current logged user is a Friend to user in database
                    {
                        if (reader.GetString(5) == "Sent")                                                                //Invite was sent to logged in user i.e Received
                        {
                            LoadReceivedInvites(reader.GetInt32(4).ToString(), reader.GetString(6), reader.GetString(7)); //hence we load it in the received list
                            receivedRequests.Add(reader.GetInt32(4).ToString());
                        }//Invite accepted
                         //else if (reader.GetString(5) == "Sent")//Invite sent
                         //{
                         //    ulPending.InnerHtml += "<li class='list-group-item list-group-item-action' style='color: #7c2020'>" +
                         //                           "<a href='#' >" +
                         //                           "<img ID='NavPhoto" + reader.GetInt32(4) + "' class='border-light rounded rounded-circle text-center' src='data:image/" + image.GetType().ToString() + ";base64," + Convert.ToBase64String(imageArray) + "' style='background-color:white; Width:50px; Height:50px; margin-right:10px' />" +
                         //                           reader.GetString(1) + " " + reader.GetString(2) +
                         //                           "<span class='float-right fas  fa-check' style='color:yellow'></span></a>" +
                         //                           "</li>";

                        //}//Invite sent
                        else if (reader.GetString(5) == "Accepted")//Invite received
                        {
                            LoadFriends(reader.GetInt32(4).ToString(), reader.GetString(6), reader.GetString(7));
                            friends.Add(reader.GetInt32(4).ToString());
                        }//Invite received else
                    }
                    else
                    {
                        try
                        {
                            if (reader.GetInt32(0) != int.Parse(Session["LoggedInUserID"].ToString()) &&
                                !friends.Contains(reader.GetInt32(0).ToString()) &&
                                !registeredPeople.Select(p => p.User_ID).Contains(reader.GetInt32(0)) &&
                                !sentRequests.Contains(reader.GetInt32(0).ToString()) &&
                                !receivedRequests.Contains(reader.GetInt32(0).ToString()))
                            {
                                registeredPeople.Add(new User()
                                {
                                    User_ID = reader.GetInt32(0), FirstName = reader.GetString(1), Surname = reader.GetString(2)
                                });
                            }
                        }
                        catch
                        {
                        }
                    }
                }//Null Friend_ID try
                catch //skip
                {
                    try
                    {
                        if (reader.GetInt32(0) != int.Parse(Session["LoggedInUserID"].ToString()) &&
                            !friends.Contains(reader.GetInt32(0).ToString()) &&
                            !registeredPeople.Select(p => p.User_ID).Contains(reader.GetInt32(0)) &&
                            !sentRequests.Contains(reader.GetInt32(0).ToString()) &&
                            !receivedRequests.Contains(reader.GetInt32(0).ToString()))
                        {
                            registeredPeople.Add(new User()
                            {
                                User_ID = reader.GetInt32(0), FirstName = reader.GetString(1), Surname = reader.GetString(2)
                            });
                        }
                    }
                    catch//skip
                    {
                    }
                }
            }//while

            reader.Close();
            con.Close();

            foreach (string userid in registeredPeople.Select(p => p.User_ID.ToString()).Except(friends).Except(sentRequests).Except(receivedRequests).ToList())
            {
                User user = registeredPeople.SingleOrDefault(u => u.User_ID == int.Parse(userid));
                LoadRegisteredPeople(user.User_ID.ToString(), user.FirstName, user.Surname);
            }
        }
    }
Example #5
0
        public void ProcessFile(string filename, string outputFilename)
        {
            try
            {
                System.Drawing.Image image = System.Drawing.Image.FromFile(filename);

                Console.WriteLine("Size {0} {1} {2}", image.Width, image.Height, image.GetType().ToString());

                float desiredWidthMM     = options.DesiredWidth;
                float stepSize           = options.StepSize;
                int   desiredPixelWidth  = (int)(desiredWidthMM / stepSize);
                int   desiredPixelHeight = image.Height * desiredPixelWidth / image.Width;
                float minThick           = options.MinThickness;
                float maxThick           = options.MaxThickness;

                Bitmap bm = new Bitmap(desiredPixelWidth, desiredPixelHeight);
                using (Graphics g = Graphics.FromImage(bm))
                {
                    g.DrawImage(image, 0, 0, desiredPixelWidth, desiredPixelHeight);
                }

                var gb = new GaussianBlur(bm);
                gb.Process(10);

                var   grayScale = new float[bm.Width, bm.Height];
                float maxGray   = 0.0f;
                float minGray   = 10.0f;
                float avgGray   = 0.0f;

                for (int i = 0; i < bm.Width; i++)
                {
                    for (int j = 0; j < bm.Height; j++)
                    {
                        Color oc           = bm.GetPixel(i, j);
                        float grayScaleVal = (float)((oc.R * 0.3) + (oc.G * 0.59) + (oc.B * 0.11)) / 256.0f;
                        grayScale[options.MirrorX ? bm.Width - 1 - i : i, j] = grayScaleVal;
                        if (grayScaleVal < minGray)
                        {
                            minGray = grayScaleVal;
                        }
                        if (grayScaleVal > maxGray)
                        {
                            maxGray = grayScaleVal;
                        }
                        avgGray += grayScaleVal;
                    }
                }

                avgGray /= bm.Width * bm.Height;

                Console.WriteLine("Min gray {0}, Max gray {1}, Average {2}", minGray, maxGray, avgGray);

                heights = new float[bm.Width, bm.Height];
                for (int i = 0; i < bm.Width; i++)
                {
                    for (int j = 0; j < bm.Height; j++)
                    {
                        float g = (grayScale[i, j] - minGray) / (maxGray - minGray);

                        if (options.Negative)
                        {
                            g = 1.0f - g;
                        }

                        float height = g * (maxThick - minThick);
                        //heights[bm.Width - 1 - i, x] = height < minThick ? minThick : height;
                        heights[bm.Width - 1 - i, j] = maxThick - height;
                    }
                }

                if (options.Binary)
                {
                    bw = new BinaryWriter(new FileStream(outputFilename != null ? outputFilename : (Path.GetFileNameWithoutExtension(filename) + ".stl"), FileMode.Create, FileAccess.ReadWrite));
                }
                else
                {
                    tw = new StreamWriter(outputFilename != null ? outputFilename : (Path.GetFileNameWithoutExtension(filename) + ".stl"));
                }

                nTriangles = 0;

                width  = (bm.Width - 1) * stepSize;
                height = (bm.Height - 1) * stepSize;

                OutputHeader();

                OutputFront(stepSize, bm);
                OutputFanBack(stepSize, bm);

                if (options.AddBorder)
                {
                    OutputTopBorder(stepSize, bm);
                    OutputBottomBorder(stepSize, bm);
                    OutputLeftBorder(stepSize, bm);
                    OutputRightBorder(stepSize, bm);
                }
                else
                {
                    OutputTop(stepSize, bm);
                    OutputBottom(stepSize, bm);
                    OutputLeft(stepSize, bm);
                    OutputRight(stepSize, bm);
                }

                OutputTrailer();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }