特定尺寸的自动裁剪图像 [英] Auto Crop Image on specific Size

查看:63
本文介绍了特定尺寸的自动裁剪图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的兄弟和朋友,

我需要帮助从后面的代码中裁剪图片,其宽度为250英尺,高度为150,因此无论图像用户大小如何上传我们的代码都必须以这个尺寸裁剪,但不能更好地从中心裁剪。

请帮助感谢您的建议和建议。

Dear Brothers & Friend,
I need help to crop Image from code behind with specific size that is 250 in width and 150 in height so what ever size of image user is uploading our code has to crop in this size but not resize better to crop from center.
please help thanks for your advice and suggestion.

推荐答案

我试过裁剪一个图像,无法做到。所以我选择制作一个新的图像。

下面的文字是我用来完成任务的思考过程,我认为这是你最接近的任务,不使用Photoshop或其他东西。我相信那里的人有比我更好的解决方案。





你必须上传图像,

看一看上传图片的大小,

然后得到它的大小,考虑计算的宽高比

画一个画布,设置背面颜色

根据上传的图片尺寸创建新图片,确保它小于目标尺寸

在画布中设置新图片

保存图像



查看图像

I tried cropping an image, could not do it. So instead I choose to make a new image.
The below text is the thinking process I used to complete the tasks, which I think is the closest you will get, without using Photoshop or something. I'm sure someone out there has a better solution than mine here.


You have to upload the image fist,
take a peek at the size of the uploaded image,
then get it's size, consider the aspect ratio for calculating
Draw a canvas, set the back color
create a new image based on the uploaded image size, make sure it's smaller than the target size
Set the new image in the canvas
Save the image

Peek at the image
Dim fs As FileStream = New FileStream(p_PhysicalPath, FileMode.Open, FileAccess.Read)
Dim image As System.Drawing.Image = System.Drawing.Image.FromStream(fs)
iWidth = image.Width
iHeight = image.Height





完成计算后,再创建新图像。请注意如何通过了解像素位置,从图像中获取大量信息,例如图像中每个像素的颜色。



要检查的示例代码



Once you have your calculations, you then create the new image. Take note of how you can get tons of information from an image such at the color of every pixel in the image by knowing the pixel location.

Sample code to examine

Dim sourceFile As Drawing.Bitmap = Nothing
Dim convertedfile As Drawing.Bitmap = Nothing
Dim newGraphic As Drawing.Graphics = Nothing

'Create a brand new image, for preview purposes
Try

   Dim staticBGColor As Drawing.Color = System.Drawing.Color.White
   Dim peekImage As Drawing.Bitmap = New Drawing.Bitmap(p_PhysicalPath, True)
   Dim bgColor As System.Drawing.Color = peekImage.GetPixel(1, 1) <- Take Note
   peekImage.Dispose()
   peekImage = Nothing

   convertedfile = New Drawing.Bitmap(cWidth, cHeight,Drawing.Imaging.PixelFormat.Format24bppRgb)
   convertedfile.SetResolution(72, 72)
   convertedfile.MakeTransparent()

   newGraphic = Drawing.Graphics.FromImage(convertedfile)
   newGraphic.SmoothingMode = Drawing.Drawing2D.SmoothingMode.Default
   newGraphic.InterpolationMode = Drawing.Drawing2D.InterpolationMode.High
   newGraphic.Clear(bgColor)

   sourceFile = Drawing.Bitmap.FromFile(p_PhysicalPath)

   newGraphic.DrawImage(sourceFile, newX, newY, newWidth, newHeight)
   convertedfile.Save(nPhysicalPath, Drawing.Imaging.ImageFormat.Jpeg)

Catch ex As Exception

End Try

newGraphic.Dispose()
convertedfile.Dispose()
sourceFile.Dispose()


非常感谢您的解决方案,我已经修改了一点,它对我来说非常棒。









Thank you so much for your solutions i had modified little bit and it is working superb for me.




Dim rd As New Random
Label3.Text = "MEM" + rd.Next(1000000000).ToString
Dim uploadFolder As String = Request.PhysicalApplicationPath + "photo\"
Dim x As Integer, y As Integer, width As Integer, height As Integer
x = InlineAssignHelper(y, 0)
'X, Y values for crop the image.

If FileUpload1.HasFile Then
    Dim extension As String = Path.GetExtension(FileUpload1.PostedFile.FileName)
    FileUpload1.SaveAs(uploadFolder & lblticket.Text & Label3.Text & extension)
    fu1.Text = "http://ltho-14847/newbangdam/photo/" & lblticket.Text & Label3.Text & extension

    width = 270
    height = 100
    Dim cropArea As New Rectangle(x, y, width, height)
    Dim bmpImage As New Bitmap(FileUpload1.PostedFile.InputStream)
    Dim bmpCrop As Bitmap = bmpImage.Clone(cropArea, bmpImage.PixelFormat)
    ' bmpCrop.Save("c:\temp\test.jpg")
    'bmpCrop.Save(uploadFolder & lblticket.Text & Label2.Text)
    bmpCrop.Save(Request.PhysicalApplicationPath + "photo\thumbs\" & lblticket.Text & Label3.Text & extension)
    '  fu1.Visible = False
    lblDone.Text = "Your Photo is Uploaded, Suffessfully !"

Else
    fu1.Text = "http://ltho-14847/newbangdam/photo/blankuser.jpg"
    fu1.Text = "First select a file."
End If

< br $> b $ b









Private Shared Function InlineAssignHelper(Of T)(ByRef target As T, value As T) As T
    target = value
    Return value
End Function


这篇关于特定尺寸的自动裁剪图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆