ASP.NET----プロジェクト中の画像処理類

11818 ワード

        #region     



        /// <summary>

        ///      

        /// </summary>

        /// <param name="originalImagePath">    (    )</param>

        /// <param name="thumbnailPath">     (    )</param>

        /// <param name="width">     </param>

        /// <param name="height">     </param>

        /// <param name="mode">

        ///         

        /// HW :       (    )

        /// W :    ,      

        /// H :    ,      

        /// Cut :       (   )

        /// </param>    

        public static void MakeThumbPhoto(string originalImagePath, string thumbnailPath, int width, int height, string mode)

        {

            System.Drawing.Image originalImage = System.Drawing.Image.FromFile(originalImagePath);



            int towidth = width;

            int toheight = height;



            int x = 0;

            int y = 0;

            int ow = originalImage.Width;

            int oh = originalImage.Height;



            switch (mode)

            {

                case "HW":   

                    break;

                case "W":                  

                    toheight = originalImage.Height * width / originalImage.Width;

                    break;

                case "H":

                    towidth = originalImage.Width * height / originalImage.Height;

                    break;

                case "Cut":      

                    if ((double)originalImage.Width / (double)originalImage.Height > (double)towidth / (double)toheight)

                    {

                        oh = originalImage.Height;

                        ow = originalImage.Height * towidth / toheight;

                        y = 0;

                        x = (originalImage.Width - ow) / 2;

                    }

                    else

                    {

                        ow = originalImage.Width;

                        oh = originalImage.Width * height / towidth;

                        x = 0;

                        y = (originalImage.Height - oh) / 2;

                    }

                    break;

                default:

                    break;

            }



            //    bmp  

            System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight);



            //      

            System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);



            //        

            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;



            //     ,         

            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;



            //             

            g.Clear(System.Drawing.Color.Transparent);



            //                      

            g.DrawImage(originalImage, new System.Drawing.Rectangle(0, 0, towidth, toheight),

                new System.Drawing.Rectangle(x, y, ow, oh),

                System.Drawing.GraphicsUnit.Pixel);

            try

            {

                // jpg       

                bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg);

            }

            catch (System.Exception e)

            {

                throw e;

            }

            finally

            {

                originalImage.Dispose();

                bitmap.Dispose();

                g.Dispose();

            }

        }



        /// <summary>

        ///           

        /// </summary>

        /// <param name="path">        </param>

        /// <param name="pathSY">             </param>

        /// <param name="addText">    </param>

        public static void AddWater(string path, string pathSY, string addText)

        {

            System.Drawing.Image image = System.Drawing.Image.FromFile(path);

            System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(image);

            g.DrawImage(image, 0, 0, image.Width, image.Height);

            System.Drawing.Font f = new System.Drawing.Font("Verdana", 60);

            System.Drawing.Brush b = new System.Drawing.SolidBrush(System.Drawing.Color.Green);



            g.DrawString(addText, f, b, 35, 35);

            g.Dispose();



            image.Save(pathSY);

            image.Dispose();

        }



        /// <summary>

        ///      

        /// </summary>

        /// <param name="path">    </param>

        /// <param name="filename">     </param>

        /// <param name="watermarkFilename">     </param>

        /// <param name="watermarkStatus">      :0=    1=   2=   3=   4=   ... 9=  </param>

        /// <param name="quality">             0--100</param> 

        /// <param name="watermarkTransparency">            1--10 (10    )</param>

        public static void AddImageSignPicture(string path, string filename, string watermarkFilename, int watermarkStatus, int quality, int watermarkTransparency)

        {

            System.Drawing.Image img = System.Drawing.Image.FromFile(path);

            System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(img);



            System.Drawing.Image watermark = new System.Drawing.Bitmap(watermarkFilename);



            if (watermark.Height >= img.Height || watermark.Width >= img.Width)

            {

                return;

            }



            System.Drawing.Imaging.ImageAttributes imageAttributes = new System.Drawing.Imaging.ImageAttributes();

            System.Drawing.Imaging.ColorMap colorMap = new System.Drawing.Imaging.ColorMap();



            colorMap.OldColor = System.Drawing.Color.FromArgb(255, 0, 255, 0);

            colorMap.NewColor = System.Drawing.Color.FromArgb(0, 0, 0, 0);

            System.Drawing.Imaging.ColorMap[] remapTable = { colorMap };



            imageAttributes.SetRemapTable(remapTable, System.Drawing.Imaging.ColorAdjustType.Bitmap);



            float transparency = 0.5F;

            if (watermarkTransparency >= 1 && watermarkTransparency <= 10)

            {

                transparency = (watermarkTransparency / 10.0F);

            }



            float[][] colorMatrixElements = {

                                                new float[] {1.0f,  0.0f,  0.0f,  0.0f, 0.0f},

                                                new float[] {0.0f,  1.0f,  0.0f,  0.0f, 0.0f},

                                                new float[] {0.0f,  0.0f,  1.0f,  0.0f, 0.0f},

                                                new float[] {0.0f,  0.0f,  0.0f,  transparency, 0.0f},

                                                new float[] {0.0f,  0.0f,  0.0f,  0.0f, 1.0f}

                                            };



            System.Drawing.Imaging.ColorMatrix colorMatrix = new System.Drawing.Imaging.ColorMatrix(colorMatrixElements);



            imageAttributes.SetColorMatrix(colorMatrix, System.Drawing.Imaging.ColorMatrixFlag.Default, System.Drawing.Imaging.ColorAdjustType.Bitmap);



            int xpos = 0;

            int ypos = 0;



            switch (watermarkStatus)

            {

                case 1:

                    xpos = (int)(img.Width * (float).01);

                    ypos = (int)(img.Height * (float).01);

                    break;

                case 2:

                    xpos = (int)((img.Width * (float).50) - (watermark.Width / 2));

                    ypos = (int)(img.Height * (float).01);

                    break;

                case 3:

                    xpos = (int)((img.Width * (float).99) - (watermark.Width));

                    ypos = (int)(img.Height * (float).01);

                    break;

                case 4:

                    xpos = (int)(img.Width * (float).01);

                    ypos = (int)((img.Height * (float).50) - (watermark.Height / 2));

                    break;

                case 5:

                    xpos = (int)((img.Width * (float).50) - (watermark.Width / 2));

                    ypos = (int)((img.Height * (float).50) - (watermark.Height / 2));

                    break;

                case 6:

                    xpos = (int)((img.Width * (float).99) - (watermark.Width));

                    ypos = (int)((img.Height * (float).50) - (watermark.Height / 2));

                    break;

                case 7:

                    xpos = (int)(img.Width * (float).01);

                    ypos = (int)((img.Height * (float).99) - watermark.Height);

                    break;

                case 8:

                    xpos = (int)((img.Width * (float).50) - (watermark.Width / 2));

                    ypos = (int)((img.Height * (float).99) - watermark.Height);

                    break;

                case 9:

                    xpos = (int)((img.Width * (float).99) - (watermark.Width));

                    ypos = (int)((img.Height * (float).99) - watermark.Height);

                    break;

            }



            g.DrawImage(watermark, new System.Drawing.Rectangle(xpos, ypos, watermark.Width, watermark.Height), 0, 0, watermark.Width, watermark.Height, System.Drawing.GraphicsUnit.Pixel, imageAttributes);



            System.Drawing.Imaging.ImageCodecInfo[] codecs = System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders();

            System.Drawing.Imaging.ImageCodecInfo ici = null;

            foreach (System.Drawing.Imaging.ImageCodecInfo codec in codecs)

            {

                if (codec.MimeType.IndexOf("jpeg") > -1)

                {

                    ici = codec;

                }

            }

            System.Drawing.Imaging.EncoderParameters encoderParams = new System.Drawing.Imaging.EncoderParameters();

            long[] qualityParam = new long[1];

            if (quality < 0 || quality > 100)

            {

                quality = 80;

            }

            qualityParam[0] = quality;



            System.Drawing.Imaging.EncoderParameter encoderParam = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, qualityParam);

            encoderParams.Param[0] = encoderParam;



            if (ici != null)

            {

                img.Save(filename, ici, encoderParams);

            }

            else

            {

                img.Save(filename);

            }



            g.Dispose();

            img.Dispose();

            watermark.Dispose();

            imageAttributes.Dispose();

        }



        /// <summary>

        ///           

        /// </summary>

        /// <param name="path">        </param>

        /// <param name="pathSY">             </param>

        /// <param name="pathSYP">      </param>

        public static void AddWaterPicture(string path, string pathSY, string pathSYP)

        {

            System.Drawing.Image image = System.Drawing.Image.FromFile(path);

            System.Drawing.Image copyImage = System.Drawing.Image.FromFile(pathSYP);

            System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(image);

            g.DrawImage(copyImage, new System.Drawing.Rectangle(image.Width - copyImage.Width, image.Height - copyImage.Height, copyImage.Width, copyImage.Height), 0, 0, copyImage.Width, copyImage.Height, System.Drawing.GraphicsUnit.Pixel);

            g.Dispose();



            image.Save(pathSY);

            image.Dispose();

        }



        #endregion