Conversion between base64 bytes[] Stream in C # and conversion of PDF into pictures

Recent work has encountered the need to access external web service services to process and transform pictures and files. Now take notes of some of the commonly used methods.
Scene 1:
Web service is used to return the image content, and then process it. For convenience, most of them use base64 string to transfer, which involves various transformations. Now, the method that may be used is recorded.

// <summary>
        /// take bytes Data conversion to stream
        /// </summary>
        /// <param name="fileName">The file path to be saved</param>
        /// <param name="dataBytes">Data to be saved</param>
        /// <returns></returns>
        public static Stream BytesToStream(string fileName, byte[] dataBytes)
        {
            if (dataBytes == null)
            {
                return null;
            }
            //MemoryStream ms = new MemoryStream(dataBytes);
            using (FileStream fs = new FileStream(fileName, FileMode.OpenOrCreate))
            {
                fs.Write(dataBytes, 0, dataBytes.Length);
                return fs;
            }

        }
        /// <summary>
        /// Stream Converting to a file
        /// </summary>
        /// <param name="stream"></param>
        /// <param name="fileName"></param>
        public static void StreamToFile(Stream stream, string fileName)
        {
            // Convert Stream to byte []   
            byte[] bytes = new byte[stream.Length];
            stream.Read(bytes, 0, bytes.Length);
            // Set the location of the current stream as the beginning of the stream   
            stream.Seek(0, SeekOrigin.Begin);

            // Write byte [] to a file   
            FileStream fs = new FileStream(fileName, FileMode.Create);
            BinaryWriter bw = new BinaryWriter(fs);
            bw.Write(bytes);
            bw.Close();
            fs.Close();
        }
         public static byte[] Base64ToBytes(string base64Img)
        {
            if (!string.IsNullOrEmpty(base64Img))
            {
                byte[] bytes = Convert.FromBase64String(base64Img);
                return bytes;
            }
            return null;
        }
        /// <summary>
        /// base64 Convert to Picture
        /// </summary>
        /// <param name="base64"></param>
        /// <returns></returns>
        public static System.Drawing.Bitmap Base64ToImage(string base64)
        {
            if (!string.IsNullOrEmpty(base64))
            {
                byte[] bytes = Base64ToBytes(base64);
                if (bytes == null)
                    return null;
                System.IO.MemoryStream ms = new MemoryStream();
                ms.Write(bytes, 0, bytes.Length);
                System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(ms);
                return bmp;
            }
            return null;
        }

Scene two:
Customers return a PDF file through web service, which requires us to convert PDF into pictures (I don't know what customers think, I can't help it, I can only do so). After searching for information, there is such a third-party library, mainly using the library of O2S.Components.PDFRender4NET.dll. The specific test code links are as follows.
Convert PDF to Picture Code Password dxjf

Posted by Duje_KH on Fri, 08 Feb 2019 03:30:18 -0800