private void buttonSave_Click(object sender, EventArgs e) { var sfd = new SaveFileDialog(); sfd.Filter = "Jpeg image (*.jpg)|*.jpg|PNG image (*.png)|*.png"; if (sfd.ShowDialog() != DialogResult.OK) { return; } var ext = Path.GetExtension(sfd.FileName).ToLower(); if (ext == ".jpg") { var qualityDialog = new DialogImageQuality(); if (qualityDialog.ShowDialog() != DialogResult.OK) { return; } var encoder = GetImageEncoder(ImageFormat.Jpeg); var encoderParams = new EncoderParameters(); encoderParams.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, qualityDialog.Value); try { m_thumb.Save(sfd.FileName, encoder, encoderParams); } catch (Exception ex) { MessageBox.Show(this, "Failed to save jpeg image to \"" + sfd.FileName + "\".\n\n" + ex.ToString(), "Openplanet", MessageBoxButtons.OK, MessageBoxIcon.Error); } } else if (ext == ".png") { try { m_thumb.Save(sfd.FileName, ImageFormat.Png); } catch (Exception ex) { MessageBox.Show(this, "Failed to save png image to \"" + sfd.FileName + "\".\n\n" + ex.ToString(), "Openplanet", MessageBoxButtons.OK, MessageBoxIcon.Error); } } else { MessageBox.Show(this, "Unsupported file extension \"" + ext + "\". Use .jpg or .png instead.", "Openplanet", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
private void ReplaceImage(string fnmSource) { var qualityDialog = new DialogImageQuality(); if (qualityDialog.ShowDialog() != DialogResult.OK) { return; } var encoder = GetImageEncoder(ImageFormat.Jpeg); var encoderParams = new EncoderParameters(); encoderParams.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, qualityDialog.Value); var img = Image.FromFile(fnmSource); byte[] imgBuffer; using (var imgStream = new MemoryStream()) { using (var flippedImage = FlipImage(img)) { flippedImage.Save(imgStream, encoder, encoderParams); } imgBuffer = imgStream.ToArray(); } var newFilename = m_filename + ".openplanet"; var newSize = imgBuffer.Length; var oldSize = m_thumbSize; using (var fsOld = new BinaryReader(File.OpenRead(m_filename))) { fsOld.ReadBytes(3); // 'GBX' (magic identifier) short version = fsOld.ReadInt16(); // 6 (file version) if (version != 6) { if (MessageBox.Show(this, "Gbx version is " + version + ", which is untested on this tool. Thumbnail substitution might not work. Try anyway? (Not recommended!)", "Openplanet", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.No) { return; } } using (var fsNew = new BinaryWriter(File.Create(newFilename))) { fsNew.Write(Encoding.ASCII.GetBytes("GBX")); fsNew.Write(version); fsNew.Write(fsOld.ReadBytes(4)); // 'BUUR' (user generated flags) var typeID = fsOld.ReadUInt32(); fsNew.Write(typeID); if (typeID != TYPEID_MAP) { MessageBox.Show(this, "This doesn't look like a map file.", "Openplanet", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } int skipOffset = fsOld.ReadInt32(); // Offset to skip all of the following blocks fsNew.Write(skipOffset + (newSize - oldSize)); int numBlocks = fsOld.ReadInt32(); // Number of blocks for each property fsNew.Write(numBlocks); var oldBlocks = new List <Tuple <uint, int> >(); for (int i = 0; i < numBlocks; i++) { var blockTypeID = fsOld.ReadUInt32(); var blockSize = fsOld.ReadUInt32(); var actualBlockSize = (int)(blockSize & ~0x80000000); oldBlocks.Add(new Tuple <uint, int>(blockTypeID, actualBlockSize)); if (blockTypeID == TYPEID_MAP_THUMBNAIL) { blockSize = (uint)(actualBlockSize + (newSize - oldSize)) | (blockSize & 0x80000000); } fsNew.Write(blockTypeID); fsNew.Write(blockSize); } foreach (var pair in oldBlocks) { if (pair.Item1 == TYPEID_MAP_THUMBNAIL) { fsNew.Write(1); // ? fsNew.Write(newSize); fsNew.Write(Encoding.ASCII.GetBytes("<Thumbnail.jpg>")); fsNew.Write(imgBuffer); fsNew.Flush(); fsOld.BaseStream.Seek(m_thumbOffset + oldSize, SeekOrigin.Begin); WriteToStream(fsOld, fsNew); break; } WriteToStream(fsOld, fsNew, pair.Item2); } } } File.Delete(m_filename); File.Move(newFilename, m_filename); m_thumbSize = newSize; m_thumb.Dispose(); m_thumb = img; UpdateInterface(); }