public static FaceModelTransformType readFaceModelTransformFromFile(string filename) { using (var tr = File.OpenText(filename)) { string s = tr.ReadToEnd(); tr.Close(); string basemodelid = ""; int index = 0; double rotateX = 0, rotateY = 0, rotateZ = 0, stretchX = 0; var parts = s.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.RemoveEmptyEntries); for (int i = 0; i < parts.Length; i++) { var item = parts[i].Split('='); switch (item[0]) { case "basemodelid": basemodelid = item[1]; break; case "index": index = int.Parse(item[1]); break; case "rotateX": rotateX = double.Parse(item[1], CultureInfo.InvariantCulture); break; case "rotateY": rotateY = double.Parse(item[1], CultureInfo.InvariantCulture); break; case "rotateZ": rotateZ = double.Parse(item[1], CultureInfo.InvariantCulture); break; case "stretchX": stretchX = double.Parse(item[1], CultureInfo.InvariantCulture); break; } } FaceModelTransformType ft = new FaceModelTransformType() { index = index, basemodelid = basemodelid, rotateX = rotateX, rotateY = rotateY, rotateZ = rotateZ, stretchX = stretchX }; return(ft); } }
private void btnOK_Click(object sender, RoutedEventArgs e) { //newmesh directory string newmeshdir = AppDomain.CurrentDomain.BaseDirectory + "newmesh\\"; string meshfilename = ""; string texturefilename = ""; string infofilename = ""; string billboardfilename = ""; string configfilename = ""; //find the index and next name for the meshid int index = 0; bool bfound = false; //bool to load new face model try { if (FaceModelTransform.basemodelid != null) { if (MessageBox.Show("Save Edited face model as New face model?", "", MessageBoxButton.YesNo, MessageBoxImage.None, MessageBoxResult.No) != MessageBoxResult.Yes) { bfound = true; index = FaceModelTransform.index; } else { FaceModelTransform = new FaceModelTransformType(); } } while (!bfound) { index++; string testfile = newmeshdir + "mesh" + BaseModelID + index; // meshfilename = testfile + ".x"; //texturefilename = testfile + ".png"; //billboardfilename = testfile + ".jpg"; //infofilename = testfile + ".info.txt"; //configfilename = testfile + ".config.txt"; if (!File.Exists(testfile + ".x")) { bfound = true; } } FaceModelCreatedIndex = index; string basefilename = newmeshdir + "mesh" + BaseModelID + index; meshfilename = basefilename + ".x"; texturefilename = basefilename + ".png"; billboardfilename = basefilename + ".jpg"; infofilename = basefilename + ".info.txt"; configfilename = basefilename + ".config.txt"; //create the texture file var bm = new System.Drawing.Bitmap(1920, 1080, System.Drawing.Imaging.PixelFormat.Format32bppArgb); var gbm = System.Drawing.Graphics.FromImage(bm); gbm.Clear(System.Drawing.Color.FromArgb(BaseColorARGB)); for (int i = 0; i < _TriangleIndices.Count; i = i + 3) { System.Drawing.Pen pen = new System.Drawing.Pen(System.Drawing.Color.FromArgb(255, System.Drawing.Color.Red)); //Use different pen for these triangles connected to these points //10 (center of base of upper lip) if (_TriangleIndices[i] == 10 || _TriangleIndices[i + 1] == 10 || _TriangleIndices[i + 2] == 10) { pen = System.Drawing.Pens.Green; } //14 (nose tip) if (_TriangleIndices[i] == 14 || _TriangleIndices[i + 1] == 14 || _TriangleIndices[i + 2] == 14) { pen = System.Drawing.Pens.Green; } //0 (chin) if (_TriangleIndices[i] == 0 || _TriangleIndices[i + 1] == 0 || _TriangleIndices[i + 2] == 0) { pen = System.Drawing.Pens.Green; } //328-1105 (right eye is between this 2 points) if (_TriangleIndices[i] == 1105 || _TriangleIndices[i + 1] == 1105 || _TriangleIndices[i + 2] == 1105) { pen = System.Drawing.Pens.Green; } //883-1092 (left eye is between these 2 points) if (_TriangleIndices[i] == 1092 || _TriangleIndices[i + 1] == 1092 || _TriangleIndices[i + 2] == 1092) { pen = System.Drawing.Pens.Green; } var meshpoint0 = new System.Drawing.PointF((float)_TextureCoordinates[_TriangleIndices[i]].X * (1920 - 1), (float)_TextureCoordinates[_TriangleIndices[i]].Y * (1080 - 1)); var meshpoint1 = new System.Drawing.PointF((float)_TextureCoordinates[_TriangleIndices[i + 1]].X * (1920 - 1), (float)_TextureCoordinates[_TriangleIndices[i + 1]].Y * (1080 - 1)); var meshpoint2 = new System.Drawing.PointF((float)_TextureCoordinates[_TriangleIndices[i + 2]].X * (1920 - 1), (float)_TextureCoordinates[_TriangleIndices[i + 2]].Y * (1080 - 1)); gbm.DrawPolygon(pen, new System.Drawing.PointF[] { meshpoint0, meshpoint1, meshpoint2 } ); } gbm.Dispose(); bm.Save(texturefilename, System.Drawing.Imaging.ImageFormat.Png); bm.Dispose(); bm = null; //create the billboard jpg from the display chkMesh.IsChecked = true; BitmapSource imgsrc = Image1.Source as BitmapSource; System.Drawing.Bitmap bm1 = CCommon.BitmapImage2Bitmap(imgsrc); System.Drawing.Bitmap bm2 = new System.Drawing.Bitmap(bm1.Width + 80, bm1.Height + 150, System.Drawing.Imaging.PixelFormat.Format32bppArgb); System.Drawing.Graphics gbm2 = System.Drawing.Graphics.FromImage(bm2); gbm2.Clear(System.Drawing.Color.FromArgb(BaseColorARGB)); gbm2.DrawImage(bm1, 40, 150); var sf = new System.Drawing.StringFormat(); sf.Alignment = System.Drawing.StringAlignment.Center; string str = BaseModelID.Substring(0, BaseModelID.Length - 4) + "\n" + index; gbm2.DrawString(str, new System.Drawing.Font("Arial", 50, System.Drawing.FontStyle.Bold), System.Drawing.Brushes.Black, new System.Drawing.RectangleF(20, 10, bm1.Width + 40, 140), sf ); gbm2.Dispose(); bm2.Save(billboardfilename, System.Drawing.Imaging.ImageFormat.Jpeg); bm1.Dispose(); bm1 = null; bm2.Dispose(); bm2 = null; //the xfile using (TextWriter tw = File.CreateText(meshfilename)) { foreach (var vertice in _Positions) { tw.WriteLine("{0}:{1}:{2}", vertice.X.ToString(CultureInfo.InvariantCulture), vertice.Y.ToString(CultureInfo.InvariantCulture), vertice.Z.ToString(CultureInfo.InvariantCulture)); } for (int i = 0; i < _TextureCoordinates.Count; i++) { //if (hidden_indices.IndexOf(i) < 0) //{ // tw.WriteLine("{0}:{1}", _TextureCoordinates[i].X, _TextureCoordinates[i].Y); //} //else //{ // tw.WriteLine("{0}:{1}", 0, 0); //} tw.WriteLine("{0}:{1}", _TextureCoordinates[i].X.ToString(CultureInfo.InvariantCulture), _TextureCoordinates[i].Y.ToString(CultureInfo.InvariantCulture)); } } //the config file if (File.Exists(configfilename)) { File.Delete(configfilename); } using (TextWriter tw = File.CreateText(configfilename)) { tw.WriteLine("basemodelid={0}", BaseModelID); tw.WriteLine("index={0}", index); tw.WriteLine("rotateX={0}", sliderx.Value.ToString(CultureInfo.InvariantCulture)); tw.WriteLine("rotateY={0}", slidery.Value.ToString(CultureInfo.InvariantCulture)); tw.WriteLine("rotateZ={0}", sliderz.Value.ToString(CultureInfo.InvariantCulture)); tw.WriteLine("stretchX={0}", sliderStretchX.Value.ToString(CultureInfo.InvariantCulture)); } //the info.txt file if (File.Exists(infofilename)) { File.Delete(infofilename); } using (TextWriter tw = File.CreateText(infofilename)) { tw.WriteLine("RightEye1={0},{1}", (int)(((_TextureCoordinates[328].X + _TextureCoordinates[1105].X) / 2) * 1920), (int)(((_TextureCoordinates[328].Y + _TextureCoordinates[1105].Y) / 2) * 1080) ); tw.WriteLine("LeftEye1={0},{1}", (int)(((_TextureCoordinates[883].X + _TextureCoordinates[1092].X) / 2) * 1920), (int)(((_TextureCoordinates[883].Y + _TextureCoordinates[1092].Y) / 2) * 1080) ); tw.WriteLine("Nose1={0},{1}", (int)(_TextureCoordinates[14].X * 1920), (int)(_TextureCoordinates[14].Y * 1080) ); tw.WriteLine("Mouth3={0},{1}", (int)(_TextureCoordinates[10].X * 1920), (int)(_TextureCoordinates[10].Y * 1080) ); tw.WriteLine("Chin1={0},{1}", (int)(_TextureCoordinates[0].X * 1920), (int)(_TextureCoordinates[0].Y * 1080) ); } MessageBox.Show(BaseModelID + index + " successfully " + ((FaceModelTransform.basemodelid == null)? "created":"modified") ); FaceIndexAssigned = index; this.DialogResult = true; this.Close(); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } }